要求 await
注意:此规则是
recommended 规则集的一部分。在
deno.json 中启用完整集合{
"lint": {
"rules": {
"tags": ["recommended"]
}
}
}使用 Deno CLI 启用完整集合
deno lint --rules-tags=recommended
此规则可以通过将其添加到
deno.json 中的 include 或 exclude 数组中,从而显式包含或排除在当前标签中存在的规则之外。{
"lint": {
"rules": {
"include": ["require-await"],
"exclude": ["require-await"]
}
}
}禁止没有 await 表达式或 await using 声明的异步函数。
通常情况下,使用异步函数的主要原因是在其内部使用 await 表达式或 await using 声明。如果异步函数两者都没有,则很可能是一个无意的错误。
无效示例
async function f1() {
doSomething();
}
const f2 = async () => {
doSomething();
};
const f3 = async () => doSomething();
const obj = {
async method() {
doSomething();
},
};
class MyClass {
async method() {
doSomething();
}
}
有效示例
await asyncFunction();
function normalFunction() {
doSomething();
}
async function f1() {
await asyncFunction();
}
const f2 = async () => {
await asyncFunction();
};
const f3 = async () => await asyncFunction();
async function f4() {
for await (const num of asyncIterable) {
console.log(num);
}
}
async function f5() {
using = createResource();
}
// empty functions are valid
async function emptyFunction() {}
const emptyArrowFunction = async () => {};
// generators are also valid
async function* gen() {
console.log(42);
}