Promise from two eyes to double eyes (4) – several key problems of promise (2)

Time:2020-11-10

How promise connects multiple operation tasks

Promise's then() returns a new promise, which connects multiple synchronous / asynchronous tasks through the chain call of then
new Promise((resolve, reject) => {  
  setTimeout(() => {  
    console.log (execute task 1 (asynchronous)  
    resolve(1)  
  }, 1000);  
}).then(  
  value => {  
    console.log ('result of task 1:', value)  
    console.log ('execute task 2 (synchronize)]  
    return 2  
  }  
).then(  
  value => {  
    console.log ('task of value')  
  
    return new Promise((resolve, reject) => {  
      //Start task 3 (asynchronous)  
  setTimeout(() => {  
        console.log ('execute task 3 (asynchronous))')  
        resolve(3)  
      }, 1000);  
    })  
  }  
).then(  
  value => {  
    console.log ('result of task 3:', value)  
  }  
)
Implementation results:
One second later
Execute task 1 (asynchronous)
Results of task 1: 1
Perform task 2 (synchronize)
Results of task 2: 2
再过One second later
Execute task 3 (asynchronous))
Results of task 3: 3

Promise abnormal transmission / penetration

1. When using promise's then chain call, the failed callback can be specified at the end,  
2. Any exception in the previous operation will be sent to the last failed callback for processing
new Promise((resolve, reject) => {  
  // resolve(1)  
  reject(1)  
}).then(  
  value => {  
    console.log('onResolved1()', value)  
    return 2  
  },  
  //Reason = > {throw reason} // if there is no reject callback, it is passed down like this
).then(  
  value => {  
    console.log('onResolved2()', value)  
    return 3  
  },  
  //Reason = > {throw reason} // if there is no reject callback, it is passed down like this
).then(  
  value => {  
    console.log('onResolved3()', value)  
  },  
  // reason =>  Promise.reject (reason) // if there is no reject callback, it will be passed down like this
).catch(reason => {  
  console.log('onReejected1()', reason)  
  // throw reason  
 // return Promise.reject(reason)
 }).then(  
  value => {  
    console.log('onResolved3()', value)  
  },  
  reason => {  
    console.log('onReejected2()', reason)  
  }  
)
Implementation results:
onReejected1() 1
onResolved3() undefined

Break promise chain

Break promise chain?  
1. When using promise's then chain call, interrupt in the middle and no longer call the following callback function  
2. Method: return a promise object in the pending state in the callback function
new Promise((resolve, reject) => {  
  resolve(1)  
}).then(  
  value => {  
    console.log('onResolved1()', value)  
    Return new promise (() = > {}) // returns a pending promise to interrupt the promise chain  
  },  
). then (// will not enter this callback
  value => {  
    console.log('onResolved2()', value)  
  },  
  reason => {  
    console.log('onResolved2()', reason)  
  },  
)
Implementation results:
onResolved1() 1
The code has been updated to GitHub synchronously

https://github.com/hnt815/promise

Promise series

Promise from two eyes to light (1) – Preparation
Promise: from blindness to luminescence (2) – promise basis
Promise: from blindness to luminescence (3) – several key issues of promise (1)
Promise from two eyes to double eyes (4) – several key problems of promise (2)
Promise: from blindness to illumination (5) – the overall structure of handwritten promise
Promise from two eyes to two eyes (6) – the constructor of handwritten promise
Promise from two eyes to double eyes (7) – then method and catch method of handwritten promise
Promise from two eyes to double eyes (8) – resolve, reject, all, race method of handwritten promise
Promise (9) – Async and await

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]