[Feature request] disallow async promise executor
See original GitHub issuePlease 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:
- Created 5 years ago
- Reactions:7
- Comments:6 (5 by maintainers)
Top GitHub Comments
Just added my 👍. This is now accepted!
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.