AsyncGenerator

AsyncGenerator 对象由 async generator function 返回,并且它符合 异步可迭代协议和异步迭代器协议

¥The AsyncGenerator object is returned by an async generator function and it conforms to both the async iterable protocol and the async iterator protocol.

异步生成器方法始终生成 Promise 对象。

¥Async generator methods always yield Promise objects.

AsyncGenerator 是隐藏 AsyncIterator 类的子类。

¥AsyncGenerator is a subclass of the hidden AsyncIterator class.

Try it

构造函数

¥Constructor

没有与 AsyncGenerator 构造函数相对应的 JavaScript 实体。AsyncGenerator 的实例必须从 异步生成器函数 返回:

¥There's no JavaScript entity that corresponds to the AsyncGenerator constructor. Instances of AsyncGenerator must be returned from async generator functions:

js
async function* createAsyncGenerator() {
  yield await Promise.resolve(1);
  yield await Promise.resolve(2);
  yield await Promise.resolve(3);
}
const asyncGen = createAsyncGenerator();
asyncGen.next().then((res) => console.log(res.value)); // 1
asyncGen.next().then((res) => console.log(res.value)); // 2
asyncGen.next().then((res) => console.log(res.value)); // 3

只有一个隐藏对象,它是由异步生成器函数创建的所有对象共享的原型对象。该对象通常被风格化为 AsyncGenerator.prototype,使其看起来像一个类,但它应该更合适地称为 AsyncGeneratorFunction.prototype.prototype,因为 AsyncGeneratorFunction 是一个实际的 JavaScript 实体。要了解 AsyncGenerator 实例的原型链,请参阅 AsyncGeneratorFunction.prototype.prototype

¥There's only a hidden object which is the prototype object shared by all objects created by async generator functions. This object is often stylized as AsyncGenerator.prototype to make it look like a class, but it should be more appropriately called AsyncGeneratorFunction.prototype.prototype, because AsyncGeneratorFunction is an actual JavaScript entity. To understand the prototype chain of AsyncGenerator instances, see AsyncGeneratorFunction.prototype.prototype.

实例属性

¥Instance properties

这些属性在 AsyncGenerator.prototype 上定义并由所有 AsyncGenerator 实例共享。

¥These properties are defined on AsyncGenerator.prototype and shared by all AsyncGenerator instances.

AsyncGenerator.prototype.constructor

创建实例对象的构造函数。对于 AsyncGenerator 实例,初始值为 AsyncGeneratorFunction.prototype

注意:AsyncGenerator 对象不存储对创建它们的异步生成器函数的引用。

¥Note: AsyncGenerator objects do not store a reference to the async generator function that created them.

AsyncGenerator.prototype[Symbol.toStringTag]

[Symbol.toStringTag] 属性的初始值为字符串 "AsyncGenerator"。该属性在 Object.prototype.toString() 中使用。

实例方法

¥Instance methods

还从其父级 AsyncIterator 继承实例方法。

¥Also inherits instance methods from its parent AsyncIterator.

AsyncGenerator.prototype.next()

返回 Promise,它将用 yield 表达式产生的给定值进行解析。

AsyncGenerator.prototype.return()

就像在当前暂停位置将 return 语句插入到生成器主体中一样,该语句完成生成器并允许生成器在与 try...finally 块组合时执行任何清理任务。

AsyncGenerator.prototype.throw()

就像在生成器主体的当前挂起位置插入一条 throw 语句一样,它通知生成器错误情况并允许其处理错误,或者执行清理并自行关闭。

示例

¥Examples

异步生成器迭代

¥Async generator iteration

以下示例迭代异步生成器,以递减的时间间隔将值 1–6 记录到控制台。请注意每次 Promise 是如何产生的,但它会在 for await...of 循环中自动解析。

¥The following example iterates over an async generator, logging values 1–6 to the console at decreasing time intervals. Notice how each time a Promise is yielded, but it's automatically resolved within the for await...of loop.

js
// An async task. Pretend it's doing something more useful
// in practice.
function delayedValue(time, value) {
  return new Promise((resolve /*, reject*/) => {
    setTimeout(() => resolve(value), time);
  });
}

async function* generate() {
  yield delayedValue(2000, 1);
  yield delayedValue(1000, 2);
  yield delayedValue(500, 3);
  yield delayedValue(250, 4);
  yield delayedValue(125, 5);
  yield delayedValue(50, 6);
  console.log("All done!");
}

async function main() {
  for await (const value of generate()) {
    console.log("value", value);
  }
}

main().catch((e) => console.error(e));

规范

Specification
ECMAScript Language Specification
# sec-asyncgenerator-objects

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看