Learn more about promise

Time:2022-5-16

promise. All principle

Promise. The all () method is used to wrap multiple promise instances into a new promise instance.

const p = Promise.all([p1, p2]);

Status response

The state of P is determined by P1 and P2, which are divided into two cases.

(1) Only when the states of P1 and P2 become full can the state of P become full. At this time, the return values of P1, P2 and P3 form an array and are passed to the callback function of P.

(2) As long as one of P1 and P2 is rejected, the state of P becomes rejected. At this time, the return value of the first rejected instance will be passed to the callback function of P.

Differences

p1、p2Instance.catchAnd.promise.all()of.catch

const p1 = new Promise((reslove) => {
    Reslove ('successful callback ')
}).then(res => res).catch(err => err)

const p2 = new Promise((reslove, reject) => {
    Reject ('error reported ')
}).then(res => res).catch(err => err)

export default function() {
    return Promise.all([p1, p2]).then((res) => {
        console. Log (RES) ["successful callback", "error reported"]
    }).catch((err) => {
        console.log(err)
    })
}
const p1 = new Promise((reslove) => {
    Reslove ('successful callback ')
}).then(res => res)

const p2 = new Promise((reslove, reject) => {
    Reject ('error reported ')
}).then(res => res)

export default function() {
    return Promise.all([p1, p2]).then((res) => {
        console.log(res)
    }).catch((err) => {
        console. Error in log (ERR)
    })
}

Code parsing

  1. Enter each of the parameters in the arraypromiseSuccess is returned only when all instances return success. Multiple instances are returnedpromiseObject successfully returns an array of values;
  2. promise.allIf one of the instances in the parameter is a failed callback, you want to execute it.thenMethod, the failed instance needs to be used.catchMethod, which will get the results of failure and success. The returned result is an array; If you don’t want to do it.thenMethod, the instance object in the parameter is not used.catchYes
  3. The input parameter is an array that canPass basic type value, orpromiseObject.

Promise. Race usage

As the name suggests, promse Race means race, which means promise Which result in race ([P1, P2, P3]) gets faster, it will return that result, regardless of whether the result itself is in success or failure status.

let p1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('success')
  },1000)
})

let p2 = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject('failed')
  }, 500)
})

Promise.race([p1, p2]).then((result) => {
  console.log(result)
}).catch((error) => {
  console. Log (error) // open 'failed'
})

Recommended Today

Illustrated big data | practical case MapReduce big data statistics

Author:Han [email protected] address:http://www.showmeai.tech/tutorials/84Article address:http://www.showmeai.tech/article-detail/170Notice: All Rights Reserved. Please contact the platform and the author for reprint and indicate the source 1. Introduction This tutorial showmeai will explain in detail the method of Hadoop using map reduce for data statistics. For the basic knowledge of Hadoop and map reduce, you can review the basic knowledge of […]