Rule proposal: report dangling `.then` calls in async functions
See original GitHub issuePlease describe what the rule should do:
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:
// invalid:
async function invalid() {
foo().then(bar);
foo().then(result => {
bar(result);
});
foo().then(result => {
bar(result);
}).catch(doSomethingElse);
}
// valid:
async function valid() {
await foo().then(bar);
await foo().then(result => {
bar(result);
});
return foo().then(result => {
bar(result);
}).catch(doSomethingElse); // (unsure about this case -- maybe `catch` could be opt-out)
}
Why should this rule be included in ESLint (instead of a plugin)?
This rule would flag a common error that developers make when they start working with promises and async functions. The invalid
examples are typically bugs, because:
- If the
foo()
promise rejects, the error cannot be caught anywhere and might silently be ignored depending on the environment. - The promise returned by
foo().then(...)
might still be pending when the surrounding function returns. - In most cases, developers intend for errors to be handled somewhere and not silently ignored.
- If the developer actually did intend to swallow errors, they should make that intention explicit by using
.catch
or a comment, so that the code doesn’t look like it has a bug.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
async function - JavaScript - MDN Web Docs - Mozilla
Code after each await expression can be thought of as existing in a .then callback. In this way a promise chain is progressively...
Read more >async/await: nowait keyword? #13376 - microsoft/TypeScript
I have a large project that has now been migrated over from javascript to typescript and we're very pleased with the results.
Read more >Async/await - The Modern JavaScript Tutorial
Let's emphasize: await literally suspends the function execution until the promise settles, and then resumes it with the promise result. That ...
Read more >SE-0296: async/await - Proposal Reviews - Swift Forums
The review of SE-0296 — async/await begins now and runs through ... each async function call would be able to report progress independently....
Read more >How do I return the response from an asynchronous call?
Whenever you call a method that returns a promise, the then handlers are always executed asynchronously - that is, after the code below...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hmm… I’m not sure if this rule is valuable because I think it’s rare cases that people use a mix of
.then
andawait
. I probably support if the rule disallows all.then
in async functions, but…About C#, Visual Studio reports function calls that their return type is
Task
/Task<T>
(E.g. async methods) if the call is not inawait
expressions. The feature has caught async function calls which are withoutawait
. It’s very useful personally. On the other hand, ESLint does not know the return type of functions. Yes,.then()
probably returns aPromise
. But I’m not sure about the value of reporting the restricted case.Closing because there doesn’t seem to be much interest in this.