Promise Abstract Operation

PromiseCapability Records

PromiseCapability Record 是一个记录值,用于封装Promise或类似promise的对象,以及能够解析或拒绝该promise的函数。 PromiseCapability RecordNewPromiseCapability 抽象操作生成。

PromiseCapability Records 具有下表的一些fields

Field NameValueMeaning
[[Promise]]an ObjectAn object that is usable as a promise.
[[Resolve]]an function objectThe function that is used to resolve the given promise.
[[Reject]]an function objectThe function that is used to reject the given promise.

IfAbruptRejectPromise 是使用PromiseCapability记录的算法步骤序列的缩写。

PromiseReaction Records

PromiseReaction Record 是一个记录值,用于存储有关promise以给定值解决或拒绝时应如何反应的信息PromiseReaction RecordPerformPromiseThen 抽象操作创建,并由NewPromiseReactionJob返回的抽象闭包使用。

PromiseReaction Records 具有下表的一些fields

Field NameValueMeaning
[[Capability]]a PromiseCapability Record or undefinedThe capabilities of the promise for which this record provides a reaction handler.
[[Type]]FULFILL or REJECTThe [[Type]] is used when [[Handler]] is EMPTY to allow for behaviour specific to the settlement type.
[[Handler]]a JobCallback Record or EMPTYThe function that should be applied to the incoming value, and whose return value will govern what happens to the derived promise. If [[Handler]] is EMPTY, a function that depends on the value of [[Type]] will be used instead.

CreateResolvingFunctions

抽象操作 CreateResolvingFunctions 接收一个promise类型的参数, 并且返回一个[[Resolve]] (a function object) and [[Reject]] (a function object) 的 Record fields

It performs the following steps when called:

  1. Let alreadyResolved be the Record [[Value]]: false.
  2. Let stepsResolve be the algorithm steps defined in Promise Resolve Functions.
  3. Let lengthResolve be the number of non-optional parameters of the function definition in Promise Resolve Functions.
  4. Let resolve be CreateBuiltinFunction(stepsResolve, lengthResolve, "", « [[Promise]], [[AlreadyResolved]] »).
  5. Set resolve.[[Promise]] to promise.
  6. Set resolve.[[AlreadyResolved]] to alreadyResolved.
  7. Let stepsReject be the algorithm steps defined in Promise Reject Functions.
  8. Let lengthReject be the number of non-optional parameters of the function definition in Promise Reject Functions.
  9. Let reject be CreateBuiltinFunction(stepsReject, lengthReject, "", « [[Promise]], [[AlreadyResolved]] »).
  10. Set reject.[[Promise]] to promise.
  11. Set reject.[[AlreadyResolved]] to alreadyResolved.
  12. Return the Record [[Reject]]: reject.

Promise Reject Functions

A promise reject function is an anonymous built-in function that has [[Promise]] and [[AlreadyResolved]] internal slots.

When a promise reject function is called with argument reason, the following steps are taken:

  1. Let F be the active function object.
  2. Assert: F has a [[Promise]] internal slot whose value is an Object.
  3. Let promise be F.[[Promise]].
  4. Let alreadyResolved be F.[[AlreadyResolved]].
  5. If alreadyResolved.[[Value]] is true, return undefined.
  6. Set alreadyResolved.[[Value]] to true.
  7. Perform RejectPromise(promise, reason).
  8. Return undefined.

The "length" property of a promise reject function is 1𝔽.

Promise Resolve Functions

A promise resolve function is an anonymous built-in function that has [[Promise]] and [[AlreadyResolved]] internal slots.

When a promise resolve function is called with argument resolution, the following steps are taken:

  1. Let F be the active function object.
  2. Assert: F has a [[Promise]] internal slot whose value is an Object.
  3. Let promise be F.[[Promise]].
  4. Let alreadyResolved be F.[[AlreadyResolved]].
  5. If alreadyResolved.[[Value]] is true, return undefined.
  6. Set alreadyResolved.[[Value]] to true.
  7. If SameValue(resolution, promise) is true, then a. Let selfResolutionError be a newly created TypeError object. b. Perform RejectPromise(promise, selfResolutionError). c. Return undefined.
  8. If resolution is not an Object, then a. Perform FulfillPromise(promise, resolution). b. Return undefined.
  9. Let then be Completion(Get(resolution, "then")).
  10. If then is an abrupt completion, then a. Perform RejectPromise(promise, then.[[Value]]). b. Return undefined.
  11. Let thenAction be then.[[Value]].
  12. If IsCallable(thenAction) is false, then a. Perform FulfillPromise(promise, resolution). b. Return undefined.
  13. Let thenJobCallback be HostMakeJobCallback(thenAction).
  14. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).
  15. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
  16. Return undefined.

The "length" property of a promise resolve function is 1𝔽.

FulfillPromise

抽象操作符 FulfillPromise 接收参数promise和value并且返回UNUSED

执行以下步骤:

  1. Assert: The value of promise.[[PromiseState]] is PENDING.
  2. Let reactions be promise.[[PromiseFulfillReactions]].
  3. Set promise.[[PromiseResult]] to value.
  4. Set promise.[[PromiseFulfillReactions]] to undefined.
  5. Set promise.[[PromiseRejectReactions]] to undefined.
  6. Set promise.[[PromiseState]] to FULFILLED.
  7. Perform TriggerPromiseReactions(reactions, value).
  8. Return UNUSED.

NewPromiseCapability ( C )

The abstract operation NewPromiseCapability takes argument C (an ECMAScript language value) and returns either a normal completion containing a PromiseCapability Record or a throw completion. It attempts to use C as a constructor in the fashion of the built-in Promise constructor to create a promise and extract its resolve and reject functions. The promise plus the resolve and reject functions are used to initialize a new PromiseCapability Record. It performs the following steps when called:

  1. If IsConstructor(C) is false, throw a TypeError exception.
  2. NOTE: C is assumed to be a constructor function that supports the parameter conventions of the Promise constructor (see 27.2.3.1).
  3. Let resolvingFunctions be the Record [[Reject]]: undefined.
  4. Let executorClosure be a new Abstract Closure with parameters (resolve, reject) that captures resolvingFunctions and performs the following steps when called: a. If resolvingFunctions.[[Resolve]] is not undefined, throw a TypeError exception. b. If resolvingFunctions.[[Reject]] is not undefined, throw a TypeError exception. c. Set resolvingFunctions.[[Resolve]] to resolve. d. Set resolvingFunctions.[[Reject]] to reject. e. Return undefined.
  5. Let executor be CreateBuiltinFunction(executorClosure, 2, "", « »).
  6. Let promise be ? Construct(C, « executor »).
  7. If IsCallable(resolvingFunctions.[[Resolve]]) is false, throw a TypeError exception.
  8. If IsCallable(resolvingFunctions.[[Reject]]) is false, throw a TypeError exception.
  9. Return the PromiseCapability Record [[Reject]]: resolvingFunctions.[[Reject]].

Note

This abstract operation supports Promise subclassing, as it is generic on any constructor that calls a passed executor function argument in the same way as the Promise constructor. It is used to generalize static methods of the Promise constructor to any subclass.

IsPromise ( x )

The abstract operation IsPromise takes argument x (an ECMAScript language value) and returns a Boolean. It checks for the promise brand on an object. It performs the following steps when called:

  1. If x is not an Object, return false.
  2. If x does not have a [[PromiseState]] internal slot, return false.
  3. Return true.

RejectPromise ( promise, reason )

The abstract operation RejectPromise takes arguments promise (a Promise) and reason (an ECMAScript language value) and returns UNUSED. It performs the following steps when called:

  1. Assert: The value of promise.[[PromiseState]] is PENDING.
  2. Let reactions be promise.[[PromiseRejectReactions]].
  3. Set promise.[[PromiseResult]] to reason.
  4. Set promise.[[PromiseFulfillReactions]] to undefined.
  5. Set promise.[[PromiseRejectReactions]] to undefined.
  6. Set promise.[[PromiseState]] to REJECTED.
  7. If promise.[[PromiseIsHandled]] is false, perform HostPromiseRejectionTracker(promise, "reject").
  8. Perform TriggerPromiseReactions(reactions, reason).
  9. Return UNUSED.

TriggerPromiseReactions ( reactions, argument )

The abstract operation TriggerPromiseReactions takes arguments reactions (a List of PromiseReaction Records) and argument (an ECMAScript language value) and returns UNUSED. It enqueues a new Job for each record in reactions. Each such Job processes the [[Type]] and [[Handler]] of the PromiseReaction Record, and if the [[Handler]] is not EMPTY, calls it passing the given argument. If the [[Handler]] is EMPTY, the behaviour is determined by the [[Type]]. It performs the following steps when called:

  1. For each element reaction of reactions, do a. Let job be NewPromiseReactionJob(reaction, argument). b. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
  2. Return UNUSED.

HostPromiseRejectionTracker ( promise, operation )

The host-defined abstract operation HostPromiseRejectionTracker takes arguments promise (a Promise) and operation ("reject" or "handle") and returns UNUSED. It allows host environments to track promise rejections.

The default implementation of HostPromiseRejectionTracker is to return UNUSED.

Note

HostPromiseRejectionTracker is called in two scenarios: When a promise is rejected without any handlers, it is called with its operation argument set to "reject". When a handler is added to a rejected promise for the first time, it is called with its operation argument set to "handle". A typical implementation of HostPromiseRejectionTracker might try to notify developers of unhandled rejections, while also being careful to notify them if such previous notifications are later invalidated by new handlers being attached.

Note

If operation is "handle", an implementation should not hold a reference to promise in a way that would interfere with garbage collection. An implementation may hold a reference to promise if operation is "reject", since it is expected that rejections will be rare and not on hot code paths.