ai js promise lambda 2026-09-01
ai js promise lambda 2026-09-01
in js promise constructor, whats the spec for its parameter. show complete spec.
(ai answer)
The Promise constructor is specified as Promise ( executor ) in ECMA-262. The only parameter is executor.
Formal requirement on the parameter
executor must be callable. If IsCallable(executor) is false, the constructor throws a TypeError.
It is invoked synchronously during construction as:
Call(executor, undefined, « resolve, reject »)
thisisundefined- first argument is the resolve function
- second argument is the reject function
If that call completes abruptly (the executor throws), the new promise is rejected with the thrown value. The executor’s return value is ignored.
Complete constructor algorithm (current ECMA-262)
27.2.3.1 Promise ( executor )
This function performs the following steps when called:
- If NewTarget is undefined, throw a TypeError exception.
- If IsCallable(executor) is false, throw a TypeError exception.
- Let promise be ? OrdinaryCreateFromConstructor(NewTarget,
"%Promise.prototype%", « [[PromiseState]], [[PromiseResult]], [[PromiseFulfillReactions]], [[PromiseRejectReactions]], [[PromiseIsHandled]] »). - Set promise.[[PromiseState]] to pending.
- Set promise.[[PromiseResult]] to empty.
- Set promise.[[PromiseFulfillReactions]] to a new empty List.
- Set promise.[[PromiseRejectReactions]] to a new empty List.
- Set promise.[[PromiseIsHandled]] to false.
- Let resolvingFunctions be CreateResolvingFunctions(promise).
- Let completion be Completion(Call(executor, undefined, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »)).
- If completion is an abrupt completion, then
- Perform ? Call(resolvingFunctions.[[Reject]], undefined, « completion.[[Value]] »).
- Return promise.
Note
The executor argument must be a function object. It is called for initiating and reporting completion of the possibly deferred action represented by this Promise. The executor is called with two arguments: resolve and reject. These are functions that may be used by the executor function to report eventual completion or failure of the deferred computation. Returning from the executor function does not mean that the deferred action has been completed but only that the request to eventually perform the deferred action has been accepted.
What resolve / reject actually are
They are the pair produced by CreateResolvingFunctions(promise): two anonymous built-in functions that share an alreadyResolved flag. After the first successful call, later calls are no-ops.
- reject(reason) — RejectPromise(promise, reason)
- resolve(resolution) — if resolution is the same promise, reject with TypeError; if it is not an object or has a non-callable
then, fulfill with it; otherwise enqueue a thenable job.
That is the complete specified contract for the constructor parameter.