Created
September 17, 2021 14:24
-
-
Save JaimeStill/302ca8beaf39115535673d3fae7b2b9e to your computer and use it in GitHub Desktop.
Demonstrate utility of async / await vs. promise.then when coordinating async calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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