简中文档

require-await

NOTE: this rule is part of the recommended rule set.
Enable full set in deno.json:
{
  "lint": {
    "rules": {
      "tags": ["recommended"]
    }
  }
}
Enable full set using the Deno CLI:
deno lint --rules-tags=recommended
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the include or exclude array in deno.json:
{
  "lint": {
    "rules": {
      "include": ["require-await"],
      "exclude": ["require-await"]
    }
  }
}

不允许没有 await 表达式或 await 使用声明的异步函数。

一般来说,使用异步函数的主要原因是在其中使用 await 表达式或 await 使用声明。如果一个异步函数两者都没有,那么它很可能是一个无意的错误。

无效:

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();
}

// 空函数是有效的
async function emptyFunction() {}
const emptyArrowFunction = async () => {};

// 生成器也是有效的
async function* gen() {
  console.log(42);
}

你找到了你需要的东西吗?

编辑此页面
隐私政策