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/proposal-faster-promise-adoption.md.
  • 简体中文
  • Faster Promise adoption S1

    中文标题:更快的 Promise 采纳(Faster Promise adoption)

    提案概览
    提案速览

    本提案旨在减少外部 Promise 采纳内部 Promise 状态所需的微任务 tick 数量,当前为 2 个 tick,导致性能比 return await 更慢。它提议为原生 Promise 采纳建立一个快速路径,同时消除重入风险,并在兼容 Web 的情况下可能加速 thenable 的采纳。

    Note

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

    更快的 Promise 采纳(proposal-faster-promise-adoption)

    “更快的 Promise 采纳”提案旨在减少外部 Promise 采纳内部 Promise 状态所需的宏任务(tick)数量。

    提案发起人:@jridgewell, @mhofman, (还有你吗?)

    作者:@jridgewell

    状态:Stage 1

    问题

    const outer = new Promise(res => {
      const inner = Promise.resolve(1);
      res(inner);
    });

    在当前规范中,外部 Promise 最终与内部 Promise 的 1 值“敲定”(settle)需要 2 个宏任务。这出现在初始 Promise 解析(如上文所示)、链式 Promise p.then(() => Promise.resolve(1)),以及异步函数中:

    // 直接返回一个 Promise 会在 2 个宏任务后敲定 `direct`。
    const direct = (async () => Promise.resolve(1))();
    
    // 返回一个已等待(awaited)的 Promise 的值会在 1 个宏任务后敲定 `awaited`。
    const awaited = (async () => await Promise.resolve(1))();

    令人惊讶的是,实际上 return await promisereturn promise 更快!

    这个问题的根源是为了防止在进行 thenable 采纳时发生同步交错,以保护执行采纳的代码免受重入风险。不幸的是,其他同步交错点仍然存在或被引入。

    提案

    我们希望为 Promise 采纳创建一个“快速路径”,使原生 Promise 能够快速采纳另一个原生 Promise 的状态,同时消除所有重入风险。如果可能,我们还希望使原生对 thenable(用户态 Promise)的采纳更快,但这只有在能够确定其与 Web 兼容,并且不允许任何同步重入的情况下才会进行。

    链接

    • 2022 年 6 月委员会会议:幻灯片,笔记(待发布)。