Promise
is an asynchronous programming solution for dealing with asynchronous operations and callback hell.
Promise
Objects have three states: pending(进行中)
, , fulfilled(已成功)
and rejected(已失败)
.
Promise
The state of an object can only be pending
changed from to fulfilled
or rejected
, and once changed, it cannot be changed again.
Promise
The object's constructor accepts as an argument a function that accepts two arguments resolve
and reject
, representing Promise
the resolution and rejection of the object, respectively.
The Promise object's then
method is used to add resolution and rejection callbacks, and returns a new Promise
object.
The method of the Promise object catch
is used to add a callback function for rejection, and returns a new Promise
object.
The method of the Promise object finally
is used to add a callback function, which Promise
will be called after the object is resolved or rejected, and returns a new Promise object.
Promise.all
The method accepts a Promise object 数组
as a parameter, returns a Promise
object that 都被解决
will be resolved after all the Promise objects passed in, and uses the results of all Promise objects as a 数组传递给 .then
callback function in the method.
Promise.race
The 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.resolve
method returns a parsed object with the given value Promise 对象
.
Promise.reject
method returns an object that was rejected with the given reason Promise 对象
.
Promise.allSettled
method 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.