question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Style proposal: async/await vs .then()

See original GitHub issue

I’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:open
  • Created 2 years ago
  • Reactions:6
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
fernandocanizocommented, May 19, 2021

To @am2222

Why not to use then().catch()?

async/await allows for cleaner code style.

Also, sometimes you need to use a result from previous calls, and it’s cleaner to do:

// ...
try {
  const aResult = await calculateA();
  const bResult = await calculateB();
  const finalResult = await calculateFinal(aResult, bResult);
  return finalResult;
} catch (e) {
  console.error(e);
  // ...
}

Than:

// ...
return calculateA()
  .then(aResult => calculateB()
    .then(bResult => calculateFinal(aResult, bResult))
  .catch(e => console.error(e));

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:

  // ...
  .then(bResult => {
    // ... some other statements
    return calculateFinal(aResult, bResult);
  })
// ...

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 an UnhandledPromiseRejectionWarning.

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.

1reaction
hagopj13commented, Jul 4, 2021

@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 avoid top-level-await and Promise.all. If you have any suggestions on how to change these as well, please go ahead and make a PR.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found