new rule: Prefer using async/await instead of Promise.prototype.then/catch/finally
See original GitHub issueIn 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:
- Created 6 years ago
- Reactions:61
- Comments:15 (11 by maintainers)

Top Related StackOverflow Question
We have something like this here as well: https://github.com/xjamundx/eslint-plugin-promise
Feel free to copy/ take whatever.
@kaicataldo I’m on it ☺️