Understanding of promise

Time:2020-11-27

What is promise?

Including the above: Abstract asynchronous operation tools.
In javascript: promise is a constructor (function is also an object)

Why promise?

1. If multiple nested asynchronous operations are used directly in the callback mode, it will cause a callback shell
2. Promise makes asynchronous operation more standardized and API more unified.
3. The complex relationship between multiple asynchronous operations can be handled by promise.

Three states of promise

Satisfied: satisfied, successful
Rejected: rejected, failed
Pending: initial state
Note: set refers to one of the states of fulfilled, rejected.

Promise method:

Promise() constructor

Promise(
    //executor
    function( resolve,reject){
    }
);

The constructor is used to generate promise objects, and the executor function is executed synchronously when the promise constructor is executed.
When the resolve and reject functions are called, set the promise status to fulfilled and rejected respectively

new Promise( (resolve, reject)=>{
    resolve( 'resolved value' );
});

new Promise( (resolve, reject)=>{
    reject( 'rejected value' );
});

Promise.resolve, Promise.reject

Promise also provides a global static method for quickly generating promise objects.
Promise.resolve Promise generating a fulfilled state
Promise.reject Generate a promise with rejected status

    Promise.resolve( 'resolved value' );
    Promise.reject( 'rejected value' );

Promise instance method: then (onfulfilled, onrejected), catch (onrejected)

Then and catch are used to handle the situation when promise fullfilled succeeds or rejected fails.
More recommended approach:
myPromise.then(onfulfilled).catch(onrejected);
Such benefits:
1. Then, catch distinguish, respectively handle success and failure, the code is easier to understand.
2. Then (). Then (). Then (). Catch(); then, catch can catch the exceptions in the previous then.

Why promise can be called chained?

because Promise.prototype.then Methods and Promise.prototype.catch Methods return promise objects. So you can chain call:

myPromise.then(xxx).then(xxx);

Promise.prototype.then What is the relationship between the promise returned by then and the return values of onfulfilled and onrejected?

The return value of onfulfilled, onrejected Then, the return value of catch
Value (common)
fulfilled Promise
fulfilled Promise
Throw error exception
rejected Promise
rejected Promise
pending Promise pending Promise

Multiple promise Association operations

There are three hypotheses for a / b
1. A can execute B only after a is executed, and C can only be executed after B is executed
The typical sequential execution relation uses chain operation directly

A.then(()=>{return B})
 .then(()=>{return C});

2. Only when a / B is successfully executed can C be executed

Promise.all([A,B]).then(
()=>{
    return C;
}
);

3. Any set led in ABC can:
Promise([A,B,C]).then(xxx).catch(xxx);
Note: after the first promise set, the execution of the remaining promise objects is not canceled.

Additional knowledge:

One Promise.resolve In addition to creating promise quickly, method can also convert thenable (object with. Then method) to promise. A typical $. Ajax () returns the thenable object.

2. The onfulfilled and onrejected methods in then (onfulfilled, onrejected) are asynchronous calls that are immediate calls Promise.prototype.then When promise is registered, promise has been set, and the two functions will not be executed synchronously. Promise will call the callback function asynchronously, which is the policy of promise design.

3. If the onrejected function is not registered, the exception will be thrown, but can not be caught.

4. Try not to use the non chain operation promise object. Under the non chain operation, the exception will not be caught and the return value of then cannot be obtained (even if there is a return value for then)

5. Use reject instead of throw to trigger project failure, because it is difficult to distinguish between rejected actively thrown and program execution error.