1 # Disallow async functions which have no `await` expression (require-await)
3 Asynchronous functions in JavaScript behave differently than other functions in two important ways:
5 1. The return value is always a `Promise`.
6 2. You can use the `await` operator inside of them.
8 The primary reason to use asynchronous functions is typically to use the `await` operator, such as this:
11 async function fetchData(processDataItem) {
12 const response = await fetch(DATA_URL);
13 const data = await response.json();
15 return data.map(processDataItem);
19 Asynchronous functions that don't use `await` might not need to be asynchronous functions and could be the unintentional result of refactoring.
21 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.
25 This rule warns async functions which have no `await` expression.
27 Examples of **incorrect** code for this rule:
30 /*eslint require-await: "error"*/
32 async function foo() {
41 Examples of **correct** code for this rule:
44 /*eslint require-await: "error"*/
46 async function foo() {
62 // Allow empty functions.
63 async function noop() {}
68 Asynchronous functions are designed to work with promises such that throwing an error will cause a promise's rejection handler (such as `catch()`) to be called. For example:
71 async function fail() {
72 throw new Error("Failure!");
75 fail().catch(error => {
76 console.log(error.message);
80 In this case, the `fail()` function throws an error that is intended to be caught by the `catch()` handler assigned later. Converting the `fail()` function into a synchronous function would require the call to `fail()` to be refactored to use a `try-catch` statement instead of a promise.
82 If you are throwing an error inside of an asynchronous function for this purpose, then you may want to disable this rule.
86 * [require-yield](require-yield.md)