Skip to content

Reduce the number of microtask ticks required to adopt the state of a promise

License

Notifications You must be signed in to change notification settings

tc39/proposal-faster-promise-adoption

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Links

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

About

Reduce the number of microtask ticks required to adopt the state of a promise

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Languages