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.

Rule proposal: report dangling `.then` calls in async functions

See original GitHub issue

Please 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mysticateacommented, Nov 16, 2017

Hmm… I’m not sure if this rule is valuable because I think it’s rare cases that people use a mix of .then and await. 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 in await expressions. The feature has caught async function calls which are without await. It’s very useful personally. On the other hand, ESLint does not know the return type of functions. Yes, .then() probably returns a Promise. But I’m not sure about the value of reporting the restricted case.

0reactions
not-an-aardvarkcommented, Nov 27, 2017

Closing because there doesn’t seem to be much interest in this.

Read more comments on GitHub >

github_iconTop 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 >

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