eslint/require-await Pedantic β
What it does β
Disallow async functions which have no await
expression.
Why is this bad? β
Asynchronous functions in JavaScript behave differently than other functions in two important ways:
- The return value is always a
Promise
. - You can use the
await
operator inside of them.
The primary reason to use asynchronous functions is typically to use the await operator, such as this:
js
async function fetchData(processDataItem) {
const response = await fetch(DATA_URL);
const data = await response.json();
return data.map(processDataItem);
}
Asynchronous functions that donβt use await might not need to be asynchronous functions and could be the unintentional result of refactoring.
Note: this rule ignores async generator functions. This is because generators yield rather than return a value and async generators might yield all the values of another async generator without ever actually needing to use await.
Example β
Examples of incorrect code for this rule:
js
async function foo() {
doSomething();
}
Examples of correct code for this rule:
js
async function foo() {
await doSomething();
}