Skip to content

docs: added note on async generator support in require-await #9795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

37 changes: 33 additions & 4 deletions packages/eslint-plugin/docs/rules/require-await.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,43 @@ import TabItem from '@theme/TabItem';
This rule extends the base [`eslint/require-await`](https://eslint.org/docs/rules/require-await) rule.
It uses type information to allow promise-returning functions to be marked as `async` without containing an `await` expression.

:::note
`yield` expressions in [async generator functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) behave differently from sync generator functions (they unwrap promises), so the base rule never checks async generator functions. On the other hand, our rule uses type information and can detect async generator functions that both never use `await` and always yield non-promise values.
:::

## Examples

Examples of **correct** code for this rule:
<Tabs>
<TabItem value="❌ Incorrect">

```ts
async function returnNumber() {
return 1;
}

async function* asyncGenerator() {
yield 1;
}

const num = returnNumber();
const callAsyncGenerator = () => asyncGenerator();
```

</TabItem>
<TabItem value="✅ Correct">

```ts
async function returnsPromise1() {
return Promise.resolve(1);
function returnNumber() {
return 1;
}

const returnsPromise2 = () => returnsPromise1();
function* syncGenerator() {
yield 1;
}

const num = returnNumber();
const callSyncGenerator = () => syncGenerator();
```

</TabItem>
</Tabs>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading