For AI agents: the complete documentation index is available at /tc39-atlas/en/llms.txt, the full documentation bundle is available at /tc39-atlas/en/llms-full.txt, and this page is available as Markdown at /tc39-atlas/en/proposals/year/pending/proposal-faster-promise-adoption.md.
  • English
  • Faster Promise adoption S1

    Proposal details
    Proposal overview

    This proposal aims to reduce the number of microtask ticks required for an outer promise to adopt the state of an inner promise, which is currently 2 ticks, leading to slower performance compared to return await. It proposes a fast path for native promise adoption while eliminating re-entrancy hazards, and potentially speeding up thenable adoption if web-compatible.

    Note

    The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.

    proposal-faster-promise-adoption

    The Faster Promise Adoption proposal aims to reduce the number of ticks required for an outer promise to adopt the state of an inner promise.

    Champions: @jridgewell, @mhofman, (and you?)

    Author: @jridgewell

    Status: Stage 1

    Problem

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

    In the current specification, it requires 2 ticks for the outer promise to finally "settle" with the inner promise's 1 value. This comes up in initial promise resolution (like above), chained promises p.then(() => Promise.resolve(1)), and with async functions:

    // Directly returning a promise settles `direct` after 2 ticks.
    const direct = (async () => Promise.resolve(1))();
    
    // Returning an awaited promise's value settles `awaited` after 1 ticks.
    const awaited = (async () => await Promise.resolve(1))();

    Surprisingly, it's actually faster to return await promise than to return promise!

    The origin of the problem was an attempt to prevent synchronous interleaving during thenable adoption to guard the code performing the adoption against re-entrancy hazards. Unfortunately other synchronous points of interleaving remained or were introduced.

    Proposal

    We'd like to make "fast path" for promise adoption that allows native promises to quickly adopt the state of another native promise, while removing all the re-entrancy hazards. If possible, we'd also like to make native adoption of thenables (userland promises) faster, but this will only be done if we can determine that it's web compatible, and does not allow for any synchronous re-entrancy.

    • June 2022 Committee Meeting: slides, notes (pending publish).