Skip to content

Instantly share code, notes, and snippets.

@JaimeStill
Created September 17, 2021 14:24
Show Gist options
  • Save JaimeStill/302ca8beaf39115535673d3fae7b2b9e to your computer and use it in GitHub Desktop.
Save JaimeStill/302ca8beaf39115535673d3fae7b2b9e to your computer and use it in GitHub Desktop.
Demonstrate utility of async / await vs. promise.then when coordinating async calls
/*
For a more formal explanation, see https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
*/
// This is not preferred
thenChaining = () => {
this.somePromise().then(value => {
this.nextPromise().then(nextValue => {
/*
So on and so forth. This gets super ugly
super quick, and makes tracing errors
more difficult than it needs to be.
*/
});
});
}
// This is preferred
asyncChaining = async () => {
const value = await this.somePromise();
const nextValue = await this.nextPromise();
/*
So on and so forth. This makes the flow of execution
easier to understand and has the added benefit of being
optimized to run faster than traditional promises.
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment