For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/stage/2/proposal-async-iterator-helpers.md.
  • 简体中文
  • Async Iterator helpers S2

    中文标题:异步迭代器助手

    提案概览
    提案速览

    该提案添加了一组异步迭代器助手方法,如 map、filter 和 reduce,以简化异步迭代器的消费。它从同步迭代器助手中拆分出来,以解决并发问题。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    异步迭代器助手

    一个关于在 ECMAScript 中帮助通用使用和消费异步迭代器的若干接口的提案。

    本提案是从 proposal-iterator-helpers 中拆分出来的,以解决与并发相关的设计问题(见下文),这些问题与同步助手无关。许多设计问题(包括方法的选择)已在那个仓库中讨论并决定,其 README 应首先阅读。

    状态

    作者:Gus Caplan, Michael Ficarra, Adam Vandolder, Jason Orendorff, Kevin Gibbons

    Champion:Michael Ficarra, Kevin Gibbons

    本提案处于 TC39 流程 的第 2 阶段。

    本提案正在修订中。 助手核心集及其高层 API 不太可能改变,但底层规范机制可能会被彻底修订。

    本提案包含以下方法:

    • Iterator.prototype.toAsync
    • AsyncIterator.from
    • AsyncIterator.prototype
      • .map
      • .filter
      • .take
      • .drop
      • .flatMap
      • .reduce
      • .toArray
      • .forEach
      • .some
      • .every
      • .find

    关于这些方法的动机和额外高层描述,请参阅 proposal-iterator-helpers

    并发

    在产生迭代器的方法(.map.filter.take.drop.flatMap)中,异步助手有机会支持_并发_。例如,在以下代码中:

    x = asyncIteratorOfUrls
      .map(u => fetch(u))
    
    await Promise.all([
      x.next(),
      x.next(),
    ])

    可能同时有两个 fetch 调用正在运行。要使这工作,助手必须被显式设计为支持此功能。本提案的原始设计基本上将助手实现为异步生成器,这缓冲了对 .next 的调用,而不是允许多个调用同时产生效果。

    本提案正在修订以至少支持上述用例。每个助手的样式的确切细节尚未决定。

    我如何访问新的内部对象?

    本提案引入了两个新的内部对象,以及在同步提案中添加的两个之外。可以如下访问它们:

    const AsyncIteratorHelperPrototype = Object.getPrototypeOf(AsyncIterator.from([]).take(0));
    const WrapForValidAsyncIteratorPrototype = Object.getPrototypeOf(AsyncIterator.from({ async next(){} }));