Skip to content

Instantly share code, notes, and snippets.

@alex-steinberg
Created August 24, 2019 14:48
Show Gist options
  • Save alex-steinberg/a00b718ded855bbe4192861a4326a44d to your computer and use it in GitHub Desktop.
Save alex-steinberg/a00b718ded855bbe4192861a4326a44d to your computer and use it in GitHub Desktop.
Async/await explanation
class Tester {
async myAsyncFn() {
const foo = await new Promise((resolve, reject) => {
console.log('foo fired async')
resolve('myAsyncFn resolved');
});
console.log('bar fired');
console.log('foo has a value: ', foo);
}
}
const myTester = new Tester();
myTester.myAsyncFn();
// foo fired async
// bar fired
// foo has a value: myAsyncFn resolved
class Tester {
myAsyncFn() {
const foo = new Promise((resolve, reject) => {
console.log('foo fired')
resolve('myAsyncFn resolved');
});
console.log('bar fired');
console.log('foo has a value: ', foo);
}
}
const myTester = new Tester();
myTester.myAsyncFn();
// foo fired
// bar fired
// foo has a value: Promise {<resolved>: "myAsyncFn resolved"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment