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.

new rule: Prefer using async/await instead of Promise.prototype.then/catch/finally

See original GitHub issue

In our company we have an unwritten rule to use async functions, instead of Promise.prototype.then/catch/finally with the advantage of having synchronous code with an asynchronous control flow. I believe that Promises are also harder to gasp for beginners, and require quite a bit of discipline, and are also prone to many misunderstandings in the order of execution and resolution, including discouraged patterns, e.g. .then(onFulfilled[, onRejected]).

in addition, performance of async functions is also on par with Promises now (https://v8project.blogspot.com/2017/02/v8-release-57.html)

we also encourage to wrap callbacks from third party modules and return Promises, if applicable (does not apply to events etc.), but that’s a different story…

Please describe what the rule should do:

Disallows the usage of Promise.prototype.then/catch/finally, and suggest to use async/await instead. I would leave the static methods (all, race, reject, resolve) to be allowed - if one doesn’t like those for whichever reason, one could just disallow the global Promise object with no-restricted-globals.

the idea might be the same (or similar) to: https://github.com/avajs/eslint-plugin-ava/blob/master/docs/rules/prefer-async-await.md https://github.com/avajs/eslint-plugin-ava/blob/master/rules/prefer-async-await.js

What category of rule is this? (place an “X” next to just one item)

[x] Enforces code style [ ] Warns about a potential error [x] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

// some hypothetical promise-returning function:
function promise() {
  return Promise.resolve(true);
}

// disallow
promise().then((bool) => ... );

// allow/suggest
const bool = await promise();

// disallow
promise().catch((err) => ...)

// allow/suggest
try {
  const bool = await promise();
}
catch (err) {
   // ... err
}

// disallow
promise().finally(() => {})

// allow/suggest
try {
  const bool = await promise();
}
finally {
   // ... cleanup
}

or in combination:

// disallow
promise()
  .then(bool => ... )
  .catch(err => ...)
  .finally(() => ...);

// allow/suggest
try {
   const bool = await promise();
}
catch (err) {
   // error handling
}
finally {
  // cleanup
}

// disallow
promise().then(...).then(...).then(...)

// allow/suggest
await ..
await ..
await ...

Why should this rule be included in ESLint (instead of a plugin)?

I can imagine that a lot of developers and companies might have a desire to use this rule, mainly for code style (synchronous code flow) as well as consistency.

edit: just fixed some missing await

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:61
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

7reactions
xjamundxcommented, Jan 27, 2018

We have something like this here as well: https://github.com/xjamundx/eslint-plugin-promise

Feel free to copy/ take whatever.

3reactions
manniLcommented, Jul 7, 2019

@kaicataldo I’m on it ☺️

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Promise.prototype.then() - JavaScript - MDN Web Docs
The then() method of a Promise object takes up to two arguments: callback functions for the fulfilled and rejected cases of the Promise....
Read more >
How to use Promise.prototype.finally() in async/await syntax?
Actually my main question was using Promise. prototype. catch() in async/await ES8 syntax, Undoubtedly Promise. prototype.
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
To handle these operations in JavaScript, a developer must use asynchronous ... of promises, and the modern practice of using async/await .
Read more >
Recreating Async/Await with Generators and Promises
So how are we able to do asynchronous things like setTimeout or fetch data from apis? Well luckily, JS can talk to the...
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