Promise will be divided into two parts, the first part is about the interpretation of the specification (basic introduction and Promise solution process), the second part is to realize the Promise according to the specification (divided into two parts), and understand the specification is the basis to realize the Promise, so it is suggested to read the interpretation of the specification first.

Chinese specification

English standard

The specification itself is more theoretical and bitter, and the interpretation will help to understand the more difficult parts by combining Promise instances

1, an overview of the

The Promise A+ specification is an open, robust, and generic JavaScript Promise standard. Developed by developers for their reference. Is the standard specification for developers to implement the Promise library.

The core of the specification is to develop the state of the Promise, state reversal rules, then method implementation, how to deal with exceptions, the Promise solution process, the rest of the static method all, race, etc., is not within the scope of the specification, is implemented by the developers themselves.

Promise represents the end result of an asynchronous operation; The primary way to interact with this is the THEN method, which registers two callback functions to receive the end value of a promise or the reason why the promise cannot be implemented (rejection).

The core Promises/A+ specification does not design how to make, resolve, and reject Promises. Instead, it focuses on providing A universal promisethenMethods.

2, terminology,

  • Promise

Is an object or function that has a then method and behaves in accordance with this specification;

Understanding: A Promise object that has the THEN method.

var promise1 = new Promise((resolve, reject) = > {
    resolve('resolve:OK~');
});

promise1.then(value= > {
    console.log(`onFulfilled:`, value);
})

Copy the code
  • thenable

Is an object or function that defines having then methods, such as Promise above;

Understanding: Objects or functions that have then methods, such as implemented promises, are thenable.

  • Value (value)

Any valid JavaScript value (including undefined, Thenable, and Promise)

Resolve (x); resolve(x)

  • Rejected due to

Indicate the reason for the rejection of the promise

var promise1 = new Promise((resolve, reject) = > {
    reject('" reason, rejected because')}); promise1.then(value= > {
    console.log(`onFulfilled:`, value);
}, reason= > {
    // There is no reason to refuse
    console.log(`onRejected:`, reason);
})
Copy the code
  • Exception

An exception is thrown with a throw statement. Exceptions are rejected, and the cause of the exception is treated as a reject

var promise1 = new Promise((resolve, reject) = > {
    new Throw Error('Error');
});

promise1.then(value= > {
    console.log(`onFulfilled:`, value);
}, reason= > {
    console.log(`onRejected:`, reason);
})
Copy the code

3, requirements


(1) The state of Promise

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

  • Wait for the state

In the waiting state, the following conditions must be met:

You can move to the execute or reject state

  • Implement state

When running, the following conditions must be met:

1. Cannot be migrated to any other state

2. Must have an immutable final value

  • Declined to state

When in the execution state, a promise must satisfy the following conditions ~ :

1. Cannot be migrated to any other state

There must be an immutable reason for rejection.

Immutability refers to identity, not to a deeper immutability;

If it is a reference type, the address of the reference is immutable, and the attribute can be changed.

/ / pieces
var gloabObj = {};
var promise = new Promise((resolve, reject) = > {
    resolve(gloabObj)
});
gloabObj.name = 'lls';
promise.then(value= > {
    console.log('What should I print here?',value);
})

/ / pieces
var gloabObjA = {}, gloabObjB = {age: 12};
var promise = new Promise((resolve, reject) = > {
    console.log(` ` 1111 = = = = 1111, gloabObj);
    resolve(gloabObj)
    console.log(` ` 2222 = = = = 2222, gloabObj);
});
console.log(` ` 3333 = = = = 3333, gloabObj);
gloabObj = gloabObjB;
console.log(` ` 4444 = = = = 4444, gloabObj);
promise.then(value= > {
    console.log(value);
})
Copy the code

(2) Then method

A promise must provide a THEN method to access its current value, final value, and reason.

Promise’s then method accepts two optional arguments that must be ignored if they are not functions

promise.then(onFulfilled, onRejected)
Copy the code
(1),onFulfilledfeatures

If ondepressing is a function,

  • It must be called when the promise execution ends, and the first argument is the promise’s final value
  • When a promise cannot be called until its execution ends,
  • It cannot be called more than once
(2),onRejectedfeatures

If onRejected is a function,

  • It must be called when the promise execution ends, and the first argument is the rejection of the promise
  • When a promise cannot be called until its execution ends,
  • It cannot be called more than once
(3), and return

The then method must return a Promise object

promise2 = promise1.then(onFulfilled, onRejected);
Copy the code
  • ifonFulfilledoronRejectedThrow an exceptione,promise2Execution must be rejected and a rejection must be returnede
var promise1 = new Promise((resolve, reject) = > {
    resolve('promise1 resolve ok~');
});

// If ondepressing or onRejected throws an exception (e), promise2 must reject and return rejection (e)
var promise2 = promise1.then(value= > {

    throw new Error('promise1 then Error')},reason= >{});Copy the code

Running results:

  • ifonFulfilledIs not a function andpromise1Successful execution,promise2Must execute successfully and return the same value
var promise1 = new Promise((resolve, reject) = > {
    resolve('promise1 resolve ok~');
});

// If 'ondepressing' is not a function and 'promise1' performs successfully, 'promise2' must perform successfully and return the same value
var promise2 = promise1.then('not function')    
Copy the code

Running results:

  • ifonRejectedIs not a function andpromise1Refuse to enforce,promise2Execution must be rejected and the same data returned
var promise1 = new Promise((resolve, reject) = > {
    reject('promise1 resolve ok~');
});

// If 'onRejected' is not a function and 'promise1' rejects execution, 'promise2' must reject execution and return the same data as' promise1 '
var promise2 = promise1.then(null.'not function')    
Copy the code

Running results:

  • ifonFulfilledoronRejectedReturn a valuex, then runPromise resolution process:[[Resolve]](promise2, x)

The Promise resolution process is detailed in the Promise A+ specification of the Promise resolution process