Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create async-await.js #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions async-await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Basic understanding for async-await, experiment!

// waiting for value
const wait = (time) => {
// setTimeout doesnt return promise so need to return one
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(time);
resolve();
}, time);
});
};

const firstTimeout = async (f) => {
console.log("run 1");
return await wait(f);
};

const secondTimeout = async () => {
console.log("run 2");
await wait(2000);
return 2000;
};

const thirdTimeout = async () => {
console.log("run 3");
await wait(3000);
return 1000;
};

const main = async () => {
console.log("hi"); // print first
// wait for value to use
// https://stackoverflow.com/questions/48617486/why-async-await-doesnt-work-in-my-case
// await only suspends when the value passed to it is a Promise.
// In your case, setTimeout returns a Number so await doesn't wait for it.
let f = await thirdTimeout();
let s = await firstTimeout(f);
secondTimeout(s);
console.log("test"); // print last
};
main();