Style proposal: async/await vs .then()
See original GitHub issueI’ve seen there’s a mix in the code where sometimes async/await
is used and sometimes somePromise.then().catch()
. I’d like to propose to homogenize the code by using always async/await
, and I offer myself to make the PR should the proposal be accepted.
Also, if it’s accepted, it should be added to the contribution guideline.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Async/Await vs Promise.then Style - Jesse Warden
I see a lot of new, veteran, and non-JavaScript developers confused about the 2 styles of writing Promises in JavaScript.
Read more >A Comparison Of async/await Versus then/catch
In JavaScript, there are two main ways to handle asynchronous code: then/catch (ES6) and async/await (ES7). These syntaxes give us the same ...
Read more >State of Async/Await in JavaScript | by Keerti Kotaru
await : It simplifies using a promise compared to the then() function syntax. Consider the following code snippet. It rewrites the above code...
Read more >Javascript - async await vs promise callback - Stack Overflow
then (func{}) style code to async await. In my example, converting from then to async await, removes the ability to query the API...
Read more >Alternate async/await proposal - dev - ReScript Forum
This proposal is a possible alternative to A proposal for async style sugar. The syntax and usage of async and await would look...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
To @am2222
async/await
allows for cleaner code style.Also, sometimes you need to use a result from previous calls, and it’s cleaner to do:
Than:
In addition, there’s a subtle thing to consider on the last snippet: if you were to use a block instead of an inline function on the anidated
.then
like:You need to ensure you’re returning the inner promise for the outer
.catch()
to catch possible errors from the inner promise. Or use a double.catch()
. Thus, this style can induce you to make errors. For example if you were refactoring the one-liner into a block and forget to return the promise and put the inner.catch()
you’ll end up with an unnoticed bug until you get anUnhandledPromiseRejectionWarning
.Consider I gave a very simple example with 2 calls. What would the code look if you need 5 or 10 different asynchronous calls under the same function?
Quoting Douglas Crockford from memory: “If one coding style can induce you to make errors and other not, then use the later”.
Aim to avoid the cognitive load always.
@fernandocanizo I have tried to (mostly) use
async/await
, except for a few exceptions, which I just counted as 4 occurances. These are mainly to avoidtop-level-await
andPromise.all
. If you have any suggestions on how to change these as well, please go ahead and make a PR.