Stage 1 Draft / April 21, 2026

MaybeDeferredPromiseResolve (name TBD)

This spec text is not reviewed yet and may have issues.

1 MaybeDeferredPromiseResolve Abstract Operations

These operations provide a mechanism for resolving a promise such that, if resolution would synchronously execute arbitrary user code, the user-code-running portion is deferred to a subsequent microtask. On return from MaybeDeferredPromiseResolve, the caller's PromiseCapability Record is guaranteed to be latched: any subsequent call to its [[Resolve]] or [[Reject]] is a no-op, regardless of whether the deferred job has yet run.

1.1 IsGetterDescriptor ( Desc )

The abstract operation IsGetterDescriptor takes argument Desc (a Property Descriptor) and returns a Boolean. It performs the following steps when called:

  1. If Desc has a [[Get]] field, return true.
  2. Return false.

1.2 HasPropertyWhichCouldRunUserCode ( o, propertyKey )

The abstract operation HasPropertyWhichCouldRunUserCode takes arguments o (an Object) and propertyKey (a property key) and returns a Boolean. It walks the prototype chain of o looking for propertyKey, returning true if retrieving the property could run user code: that is, the property is a getter, or a Proxy is encountered on the chain. It performs the following steps when called:

  1. Repeat, while o is not null,
    1. If o has a [[ProxyHandler]] internal slot, return true.
    2. Let desc be o.[[GetOwnProperty]](propertyKey).
    3. If desc is not undefined, return IsGetterDescriptor(desc).
    4. Set o to o.[[GetPrototypeOf]]().
  2. Return false.
Note

This relies on ordinary [[GetPrototypeOf]] never running user code. A Proxy is handled by short-circuiting with true before any of its MOP is invoked. Roughly corresponds to SpiderMonkey's LookupOwnPropertyPure.

1.3 RequiresDeferredPromiseResolution ( value )

The abstract operation RequiresDeferredPromiseResolution takes argument value (an ECMAScript language value) and returns a Boolean. It determines whether the synchronous steps of resolving a promise with value might execute user code. It performs the following steps when called:

  1. If value is not an Object, return false.
  2. If HasPropertyWhichCouldRunUserCode(value, "then") is true, return true.
  3. Let thenValue be ! value.[[Get]]("then").
  4. If IsCallable(thenValue) is true, return true.
  5. Return false.

1.4 PerformPromiseResolution ( promise, resolution )

The abstract operation PerformPromiseResolution takes arguments promise (a Promise) and resolution (an ECMAScript language value) and returns unused. It performs the value-inspecting steps of resolving promise with resolution. It performs the following steps when called:

  1. If resolution is not an Object, then
    1. Perform FulfillPromise(promise, resolution).
    2. Return unused.
  2. If SameValue(resolution, promise) is true, then
    1. Let selfResolutionError be a newly created TypeError object.
    2. Perform RejectPromise(promise, selfResolutionError).
    3. Return unused.
  3. Let then be Completion(Get(resolution, "then")).
  4. If then is an abrupt completion, then
    1. Perform RejectPromise(promise, then.[[Value]]).
    2. Return unused.
  5. Let thenAction be then.[[Value]].
  6. If IsCallable(thenAction) is false, then
    1. Perform FulfillPromise(promise, resolution).
    2. Return unused.
  7. Let thenJobCallback be HostMakeJobCallback(thenAction).
  8. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).
  9. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
  10. Return unused.
Note

This overlaps heavily with the Abstract Closure inside CreateResolvingFunctions. Later work could resolve this duplication

1.5 CreateResolvingFunctions ( toResolve )

The abstract operation CreateResolvingFunctions takes argument toResolve (a Promise) and returns a Record with fields [[Resolve]] (a function object) and [[Reject]] (a function object). This is a modification of the existing CreateResolvingFunctions. The resolve function takes an additional parameter doSafeResolve; when true, and resolution would synchronously execute user code, the user-code-running steps are deferred via a null-prototype wrapper thenable. It performs the following steps when called:

  1. Let promiseOrEmpty be the Record { [[Value]]: toResolve }.
  2. Let resolveSteps be a new Abstract Closure with parameters (resolution, doSafeResolve) that captures promiseOrEmpty and performs the following steps when called:
    1. If promiseOrEmpty.[[Value]] is empty, return undefined.
    2. Let promise be promiseOrEmpty.[[Value]].
    3. Set promiseOrEmpty.[[Value]] to empty.
    4. If doSafeResolve is true and RequiresDeferredPromiseResolution(resolution) is true, then
      1. Let deferredSteps be a new Abstract Closure with parameters (onFulfilled, onRejected) that captures promise and resolution and performs the following steps when called:
        1. Perform PerformPromiseResolution(promise, resolution).
        2. Return undefined.
      2. Let deferredThen be CreateBuiltinFunction(deferredSteps, 2, "", « »).
      3. Let wrapper be OrdinaryObjectCreate(null).
      4. Perform ! CreateDataPropertyOrThrow(wrapper, "then", deferredThen).
      5. Set resolution to wrapper.
    5. If SameValue(resolution, promise) is true, then
      1. Let selfResolutionError be a newly created TypeError object.
      2. Perform RejectPromise(promise, selfResolutionError).
      3. Return undefined.
    6. If resolution is not an Object, then
      1. Perform FulfillPromise(promise, resolution).
      2. Return undefined.
    7. Let then be Completion(Get(resolution, "then")).
    8. If then is an abrupt completion, then
      1. Perform RejectPromise(promise, then.[[Value]]).
      2. Return undefined.
    9. Let thenAction be then.[[Value]].
    10. If IsCallable(thenAction) is false, then
      1. Perform FulfillPromise(promise, resolution).
      2. Return undefined.
    11. Let thenJobCallback be HostMakeJobCallback(thenAction).
    12. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).
    13. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
    14. Return undefined.
  3. Let resolve be CreateBuiltinFunction(resolveSteps, 12, "", « »).
  4. Let rejectSteps be a new Abstract Closure with parameters (reason) that captures promiseOrEmpty and performs the following steps when called:
    1. If promiseOrEmpty.[[Value]] is empty, return undefined.
    2. Let promise be promiseOrEmpty.[[Value]].
    3. Set promiseOrEmpty.[[Value]] to empty.
    4. Perform RejectPromise(promise, reason).
    5. Return undefined.
  5. Let reject be CreateBuiltinFunction(rejectSteps, 1, "", « »).
  6. Return the Record { [[Resolve]]: resolve, [[Reject]]: reject }.
Note

When doSafeResolve is false (or omitted by an external caller invoking the built-in as a normal function), the behaviour is identical to the current CreateResolvingFunctions: resolution is not inspected for user-code hazards. Internal callers that want the deferred behaviour pass true.

Latching of promiseOrEmpty happens before the deferral branch, so the resolving functions are no-ops on any subsequent call, regardless of whether the deferred job has yet run.

1.6 MaybeDeferredPromiseResolve ( promiseCapability, resolution )

The abstract operation MaybeDeferredPromiseResolve takes arguments promiseCapability (a PromiseCapability Record) and resolution (an ECMAScript language value) and returns either a normal completion containing undefined or a throw completion. It resolves promiseCapability.[[Promise]] with resolution, deferring the user-code-running steps to a subsequent microtask if resolution might execute user code synchronously. In either branch, promiseCapability's resolving functions are latched on return. It performs the following steps when called:

  1. If RequiresDeferredPromiseResolution(resolution) is false, then
    1. Return ? Call(promiseCapability.[[Resolve]], undefined, « resolution »).
  2. Let promise be promiseCapability.[[Promise]].
  3. Let deferredSteps be a new Abstract Closure with parameters (onFulfilled, onRejected) that captures promise and resolution and performs the following steps when called:
    1. Perform PerformPromiseResolution(promise, resolution).
    2. Return undefined.
  4. Let deferredThen be CreateBuiltinFunction(deferredSteps, 2, "", « »).
  5. Let wrapper be OrdinaryObjectCreate(null).
  6. Perform ! CreateDataPropertyOrThrow(wrapper, "then", deferredThen).
  7. Return ? Call(promiseCapability.[[Resolve]], undefined, « wrapper »).
Note

This idea is thanks to Justin Ridgewell who suggested the wrapper idea.

The wrapper object is never exposed to user code. It is constructed solely so that resolving promise with it uses the existing thenable support for good rather than evil; we mark the promise as not eligible for overriding resolve, and a job is created for running the 'then' code.

Copyright & Software License

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.