此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
async function*
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年1月.
async function* 声明创建一个绑定到给定名称的新异步生成器函数。
你也可以使用async function* 表达式来定义异步生成器函数。
In this article
尝试一下
async function* foo() { yield await Promise.resolve("a"); yield await Promise.resolve("b"); yield await Promise.resolve("c");}let str = "";async function generate() { for await (const val of foo()) { str = str + val; } console.log(str);}generate();// Expected output: "abc"语法
async function* name(param0) { statements}async function* name(param0, param1) { statements}async function* name(param0, param1, /* …, */ paramN) { statements}备注:箭头函数不能用来定义异步生成器函数。
参数
name函数名称。
param可选函数的形参名称。有关参数的语法,请参阅函数参考。
statements可选构成函数体的语句。
描述
async function* 声明创建一个AsyncGeneratorFunction 对象。每次调用异步生成器函数时,它都会返回一个新的AsyncGenerator 对象,该对象符合异步迭代器协议。每次调用next() 都会返回一个Promise 对象,该对象会兑现为迭代器结果对象。
异步生成器函数兼具异步函数和生成器函数的特性。你可以在函数体中使用await 和yield 关键字。这使你能够使用await 优雅的地处理异步任务,同时利用生成器函数的惰性。
当从异步生成器产生一个 promsie 时,迭代器结果 promise 的最终状态将与生成器产生的 promise 状态相同。例如:
async function* foo() { yield Promise.reject(1);}foo() .next() .catch((e) => console.error(e));因为如果生成的 promise 被拒绝,迭代器的结果也将被拒绝,所以将输出1。异步生成器兑现结果的value 将不会是另一个 promise。
async function* 声明的行为类似于function 声明,它会被提升到其作用域的顶部,并且可以在其作用域的任何位置被调用,并且只能在其他上下文中被重新声明。
示例
>声明异步生成器函数
异步生成器函数总是产生结果 promise——即使每个yield 步骤是同步的。
async function* myGenerator(step) { await new Promise((resolve) => setTimeout(resolve, 10)); yield 0; yield step; yield step * 2;}const gen = myGenerator(2);gen .next() .then((res) => { console.log(res); // { value: 0, done: false } return gen.next(); }) .then((res) => { console.log(res); // { value: 2, done: false } return gen.next(); }) .then((res) => { console.log(res); // { value: 4, done: false } return gen.next(); }) .then((res) => { console.log(res); // { value: undefined, done: true } return gen.next(); });使用异步生成器函数读取一系列文件
在这个示例中,我们使用 Node 的fs/promises 模块读取一系列文件并且仅当请求时获取它的内容。
async function* readFiles(directory) { const files = await fs.readdir(directory); for (const file of files) { const stats = await fs.stat(file); if (stats.isFile()) { yield { name: file, content: await fs.readFile(file, "utf8"), }; } }}const files = readFiles(".");console.log((await files.next()).value);// 可能的输出;{ name: 'file1.txt', content: '...' }console.log((await files.next()).value);// 可能的输出:{ name: 'file2.txt', content: '...' }规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-async-generator-function-definitions> |