Skip to content

Commit

Permalink
add async await
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichimukai committed Apr 17, 2021
1 parent 1d4ecbf commit 7ba80d3
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,60 @@ asyncTask()
.catch(function onRejected(error) {
console.log(error.message);
});

Promise.resolve(1)
.then((value) => {
console.log(value);
return value * 2;
})
.then((value) => {
console.log(value);
return value * 2;
})
.then((value) => {
console.log(value);
return value * 2;
});

function delay(timeoutMs) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(timeoutMs);
}, timeoutMs);
});
}

const racePromise = Promise.race([delay(1), delay(32), delay(64), delay(128)]);

async function doAsync() {
return "value";
}

doAsync().then((value) => {
console.log(value);
});

function dummyFetch(path) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (path.startsWith("/resource")) {
resolve({ body: `Response body of ${path}` });
} else {
reject(new Error("NOT FOUND"));
}
}, 1000 * Math.random());
});
}

async function fetchAB() {
const results = [];
const responseA = await dummyFetch("/resource/A");
results.push(responseA.body);
const responseB = await dummyFetch("/resource/B");
results.push(responseB.body);
return results;
}

fetchAB().then((results) => {
console.log(results);
});

0 comments on commit 7ba80d3

Please sign in to comment.