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.

[Feature request] disallow async promise executor

See original GitHub issue

Please describe what the rule should do:

Promise executors must not be (technically, they can if you know what you’re doing) async else bad side effects happen, such as throwing (seemingly) sync errors (A) no longer rejecting the promise (thus unable to be caught by (B)), because the async function is executed in different frame:

new Promise( async r => {
    throw new Error("aaa"); // (A)
})
.catch(() => {}) // (B)

Why is this different from not catching any potential async errors in a non-async executor? It isn’t – but, the above example gives off a feeling like you’re throwing a sync error, while it’s not, and for people new to async/await this may not be evident.

More info in this SO answer.

Thus, I propose a rule that forbids async executor function.

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

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

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

new Promise( async function (resolve, reject) {
    // any code
});
new Promise( async (resolve, reject) => {
    // any code
});
const executor = async function (resolve, reject) {
    // any code
};
new Promise( executor );

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

It seems like to become common-enough error when async/await becomes more mainstream to include it in core.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
btmillscommented, Jul 23, 2018

Just added my 👍. This is now accepted!

0reactions
not-an-aardvarkcommented, Jul 22, 2018

I’ll champion this change.

@eslint/eslint-team Anyone willing to support this change? It needs two more 👍s from team members to accept it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-async-promise-executor - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
1579420 - Enable ESLint rule no-async-promise-executor on ...
When we upgraded to ESLint 6, we disabled the no-async-promise-executor rule for various files as it wasn't a quick enable. This bug is...
Read more >
JavaScript NodeJS How to use stream/promises with async ...
My advice in general is to remove as much code from within your promise executors as possible. In this case the Promise is...
Read more >
Improve async programming with JavaScript promises
A JavaScript promise can be created using the Promise constructor. The constructor takes an executor function as its argument, which is ...
Read more >
Promises chaining - The Modern JavaScript Tutorial
Returning promises allows us to build chains of asynchronous actions. Example: loadScript. Let's use this feature with the promisified ...
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