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.

`require-await` should be disabled for async generators

See original GitHub issue

What rule do you want to change? require-await

Does this change cause the rule to produce more or fewer warnings? neither

How will the change be implemented? (New option, new default behavior, etc.)? new default behavior

Please provide some example code that this change will affect:

Currently, require-await is enabled for both async functions and async generators.

The problem with this when it comes to generators is that the return value of an async generator contains symbols that a regular generator does not, making it quite different from async functions which are just regular functions that return promises.

Since generators yield rather than return a value, an async generator might yield all the values of another async generator without ever actually needing to use await.

async function * () {
  yield * anotherAsyncGenerator()
}

If you remove the async syntax the generator will fail with the following error:

TypeError: yield* (intermediate value) is not iterable

You can’t actually yield * of an async generator from a regular generator because the generator has to have the right symbols in order to be consumed properly. The VM guards against this and gives us a rather obtuse error.

IMO, it’s probably best to just disable await checking for async generators.

What does the rule currently do for this code? It fails on the required form and will not pass unless disabled.

What will the rule do after it’s changed? Not check async generators for await.

Are you willing to submit a pull request to implement this change? Nope.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dmitryrncommented, Oct 23, 2019

I’ll pick it up

1reaction
mikealcommented, Jan 11, 2020

@ljharb oh totally, i wouldn’t even know how to do that if i hadn’t had to dig through Node.js Core’s async generator support for Streams. most JS developers don’t even know Symbol is a thing 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

require-await - ESLint - Pluggable JavaScript Linter
Note: this rule ignores async generator functions. This is because generators yield rather than return a value and async generators might yield all...
Read more >
Using Async/Await with Generator Functions | Pluralsight
We need to add an asterisk somewhere between the function keyword and the name of the function. It's not important where you put...
Read more >
Is await necessary in async generator functions?
Just because an function is async doesn't mean that await is required. I think this example has async just to show that you...
Read more >
require-await | typescript-eslint
This rule extends the base eslint/require-await rule. It uses type information to add support for async functions that return a Promise .
Read more >
for await...of - JavaScript - MDN Web Docs
Iterating over async generators. Since the return values of async generator functions conform to the async iterable protocol, they can be looped ...
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