“Don’t be afraid, don’t settle, work hard in the future” — Hello! I am little sesame 😄

An open, robust, and universal JavaScript Promise standard. — Developed by developers for their reference

The primary way to interact with a Promise, which represents the end result of an asynchronous operation, is the then method, which registers two callback functions to receive the result of a Promise’s success or the reason why a Promise can’t be implemented.

Promises/A+ This specification details how to implement THE THEN method. It is the basis for Promises/A+ Promises. Therefore, this specification is very stable. Although Promises/A+ organizations may occasionally make small backward compatible changes to this specification, mainly to address specific extreme cases, if we are going to make large scale incompatible updates, we will do so with careful consideration, thorough discussion, and rigorous testing.

Historically, this specification has explicitly turned the recommendations of the previous Promise/A specification into A standard of behavior: we have expanded upon the established behavior of the original specification while eliminating some exceptions and problematic aspects of the original specification.

Finally, the core Promises/A+ specification does not design how to create, implement, and reject Promises. Instead, it focuses on providing A universal then method. The above approach to Promises may be mentioned in other codes in the future.

1. The term

1.1. A promise is an object or function that has then methods that conform to this specification.

1.2. Thenable is the object or function that defines the THEN method.

1.3. Value is any valid JavaScript value (including undefined, thenable, promise)

1.4 Exception is a value thrown using the throw statement

1.5 reason indicates a reason why a promise was rejected.

2. Request

2.1. State (states)

A Promise state must be one of the following three states: Pending, Fulfilled and Rejected.

  • 2.1.1. When a Promise is Pending

    • This is a big pity. ( This can be changed into a state of Fulfilled or Rejected.
  • This is a pity when the promise is Fulfilled

    • A Promise cannot be changed to other states

    • There must be a value that cannot be changed

  • 2.1.3 When a Promise is Rejected

    • PROMISE cannot be changed to other states

    • There must be a reason and it cannot be changed

When value or reason is not a base value, only the reference address is required to be equal, but the attribute value can be changed.

2.2. thenmethods

A promise must provide a then method to accept either its currently completed value or the reason for its rejection.

Each promise’s then method takes two arguments:

promise.then(onFulfilled, onRejected)
Copy the code
  • 2.2.1. OnFulfilled and onRejected are both optional parameters:

    • If ondepressing is not a function, it must be ignored

    • If onRejected is not a function, it must be ignored

  • 2.2.2. If ondepressing is a function:

    • This function must be called after the promise is Fulfilled, with the value of the promise as its first parameter

    • This function must not be called before the promise is Fulfilled (Fulfilled)

    • This function can only be called once

  • 2.2.3. If onRejected is a function:

    • This function must be called after the Rejected Promise, with reason as its first argument

    • This function must not be called before the Promise rejects

    • This function can only be called once

  • 2.2.4. OnFulfilled and onRejected must not be called until the implementation context stack contains only the platform code.

  • OnFulfilled and onRejected must be called as a function (i.e., without this value) [Note 3.2]

  • 2.2.6. The then method can be called multiple times within the same promise

    • If/when the promise is Fulfilled (depressing), all corresponding ondepressing call-back must be performed in sequence according to the order of THEN.

    • If/when a Promise rejects (Rejected), all corresponding onRejected callbacks must be executed in the order of THEN.

  • 2.2.7. The then method must return a Promise [note 3.3]

    promise2 = promise1.then(onFulfilled, onRejected);
    Copy the code
    • If onFulfilled or onRejected returns a value x, run the following Promise: [[Resolve]](promise2, x) [note 2.3]

    • If onFulfilled or onRejected throws an exception E, promise2 must reject (Rejected) and return the reason of exception E.

    • If ondepressing is not a function and promise1 completes (depressing) execution, promise2 must complete (depressing) execution and return the same value as promise1.

    • If onRejected is not a function and Peomise1 is in the Rejected state, promise2 must reject and return the same reason as promise1.

2.3. Promise resolution process

Promise resolution is an abstract operation that takes a Promise and a value, which we express as [[Resolve]](Promise, x). If x has a then method and looks like a Promise, The resolver attempts to make the Promise accept x’s state; Otherwise it implements the promise with the value of x.

This thenable feature makes Promise implementation more universal: as long as it exposes A THEN method that follows the Promise/A+ protocol; This also allows implementations that follow the Promise/A+ specification to coexist well with less formal but usable implementations.

To run [[Resolve]](promise, x), follow these steps:

  • 2.3.1. If a Promise and X point to the same object, reject the promise for TypeError

  • 2.3.2. If X is a Promise, take its state [note 3.4] :

    • If X is pending, the Promise shall remain pending until X is Fulfilled or Rejected. This is a big pity.

    • This is a big pity. If X is in a Fulfilled state, the promise will be Fulfilled with the same value

    • If X is in the Rejected state, reject the promise with the same reason

  • 2.3.3. Also, if x is an object or function

    • Assign X. teng to THEN [note 3.5]

    • If an error e is thrown when the value of x. teng is set, a promise is rejected for reason

    • If then is a function, call x as this with the first argument resolvePromise and the second argument rejectPromise, where:

      • If a resolvePromise is called with a value (value) y, run [[Resolve]](promise, y)

      • If reason is r, reject the rejectPromise with reason R

      • If both resolvePromise and rejectPromise are invoked at the same time, or if the same parameter is invoked more than once, the first invocation is preferred and the remaining calls are ignored

      • If calling THEN raises exception E,

        • If resolvePromise or rejectPromise has been invoked, ignore it.

        • Otherwise, e is used as the reason, reject the promise

    • If then is not a function, fulfill (fulfill) promise with x as parameter

  • 2.3.4. If x is not an object or function, fulfill (fulfill) promise with x as parameter

If a promise is resolved with a Thenable participating in the loop thenable chain, the recursive nature of [[Resolve]] (promise, thenable) will eventually result in a call to [[Resolve]] (promise, Thenable), following the above algorithm will result in infinite recursion. We encourage, but do not require, implementations to detect this recursion and reject promises as a reason for TypeError errors


3.1. “Platform code” refers to the engine, environment, and Promise implementation code. In practice, this requirement ensures that after calling the event loop of the round in which the THEN method is called, the onFulfilled and onRejected are asynchronously executed using the new stack. This can be achieved through “macro task” mechanisms (such as setTimeout or setImmediate) or “microtask” mechanisms (such as MutationObserver or process.nexttick). Because the Promise implementation is considered platform code, it may itself contain a task scheduling queue or a “trampoline” that calls the handler.

That is, in strict mode, the value of this is undefined; In non-strict mode it is a global object.

3.3. The code implementation can allow promisE2 === PROMISe1 if all requirements are met. Each implementation should document whether and under what conditions it allows PROMISE2 === PROMISe1.

3.4. In general, x is considered a true promise if it matches the current implementation. This rule allows those exceptions to accept Promises that meet known requirements.

3.5. In this step, we first store a reference to x.teng, and then test and call the reference to avoid multiple access to the x.teng property. This precaution ensures that the property is consistent because its value can be changed during retrieval calls.

3.6 The implementation should not impose a limit on the depth of the Thenable chain and assume that recursion beyond this limit is an infinite loop. Only true loop recursion should result in TypeError exceptions; If the Thenable is different across an infinite chain, recursion is always the correct behavior.