A simple understanding of Promise

Promiseis an asynchronous programming solution for dealing with asynchronous operations and callback hell.

PromiseObjects have three states: pending(进行中), , fulfilled(已成功)and rejected(已失败).

PromiseThe state of an object can only be pendingchanged from to fulfilledor rejected, and once changed, it cannot be changed again.

PromiseThe object's constructor accepts as an argument a function that accepts two arguments resolveand reject, representing Promisethe resolution and rejection of the object, respectively.

The Promise object's thenmethod is used to add resolution and rejection callbacks, and returns a new Promiseobject.

The method of the Promise object catchis used to add a callback function for rejection, and returns a new Promiseobject.

The method of the Promise object finallyis used to add a callback function, which Promisewill be called after the object is resolved or rejected, and returns a new Promise object.

Promise.allThe method accepts a Promise object 数组as a parameter, returns a Promiseobject that 都被解决will be resolved after all the Promise objects passed in, and uses the results of all Promise objects as a 数组传递给 .thencallback function in the method.

Promise.raceThe method accepts a Promise object as a parameter, returns a Promise object, which will be resolved or rejected after being 数组passed in , and the result is passed to the callback function in the method or the callback function in the method.任意一个 Promise 对象被解决或拒绝将第一个被解决或拒绝的 Promise 对象.then.catch

Promise.resolvemethod returns a parsed object with the given value Promise 对象.

Promise.rejectmethod returns an object that was rejected with the given reason Promise 对象.

Promise.allSettledmethod returns a Promise object that will not be resolved until all Promise objects passed in are resolved or rejected, and takes the result of each Promise object as an object {status: 'fulfilled', value: result} or { status: 'rejected', reason: error} Put it into an array and pass it to the callback function in the .then method.

Guess you like

Origin blog.csdn.net/weixin_43811753/article/details/130175429