Skip to content

useAwait

诊断类别:lint/suspicious/useAwait

¥Diagnostic Category: lint/suspicious/useAwait

自从:v1.4.0 来源:

¥Since: v1.4.0 Sources:

确保 async 函数使用 await

¥Ensure async functions utilize await.

此规则报告缺少 await 表达式的 async 函数。由于 async 函数返回承诺,因此通常需要使用 await 来捕获解析值并适当地处理异步操作。如果没有 await,该函数将同步运行,可能无法利用异步函数的优势。

¥This rule reports async functions that lack an await expression. As async functions return a promise, the use of await is often necessary to capture the resolved value and handle the asynchronous operation appropriately. Without await, the function operates synchronously and might not leverage the advantages of async functions.

¥Examples

¥Invalid

async function fetchData() {
// Missing `await` for the promise returned by `fetch`
return fetch('/data');
}
code-block.js:1:1 lint/suspicious/useAwait ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This async function lacks an await expression.

> 1 │ async function fetchData() {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ // Missing await for the promise returned by fetch
> 3 │ return fetch(‘/data’);
> 4 │ }
^
5 │

Remove this async modifier, or add an await expression in the function.

> 1 │ async function fetchData() {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ // Missing await for the promise returned by fetch
> 3 │ return fetch(‘/data’);
> 4 │ }
^
5 │

Async functions without await expressions may not need to be declared async.

¥Valid

async function fetchData() {
const response = await fetch('/data');
const data = await response.json();
return data;
}
// This rule does not warn about non-async functions
function processData() {
return compute(data);
}
// Nor does it warn about empty `async` functions
async function noop() { }

¥Related links