9 Asynchronous functions in JavaScript behave differently than other functions in two important ways:
11 1. The return value is always a `Promise`.
12 2. You can use the `await` operator inside of them.
14 The primary reason to use asynchronous functions is typically to use the `await` operator, such as this:
17 async function fetchData(processDataItem) {
18 const response = await fetch(DATA_URL);
19 const data = await response.json();
21 return data.map(processDataItem);
25 Asynchronous functions that don't use `await` might not need to be asynchronous functions and could be the unintentional result of refactoring.
27 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.
31 This rule warns async functions which have no `await` expression.
33 Examples of **incorrect** code for this rule:
38 /*eslint require-await: "error"*/
40 async function foo() {
51 Examples of **correct** code for this rule:
56 /*eslint require-await: "error"*/
58 async function foo() {
74 // Allow empty functions.
75 async function noop() {}
82 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:
85 async function fail() {
86 throw new Error("Failure!");
89 fail().catch(error => {
90 console.log(error.message);
94 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.
96 If you are throwing an error inside of an asynchronous function for this purpose, then you may want to disable this rule.