Interviewer: Usually handle multiple asynchronous requests, how do you do that?

Me: Generally use promise.all for processing

Interviewer: Using promise.all, have you encountered any exceptions to one of these requests? Yes. How did you deal with it?

Me: I made handwritten promises. Is that still going to beat me? (But at that time is the brain smoke, is did not think up, meet abnormal is what processing.

Here I began to reflect, handwritten Promise in the end for ha, this, will only write, not to think, deserved interview forget ๐Ÿ˜ข๐Ÿ˜ข๐Ÿ˜ข.

The previous handwritten Promise link is here

Requirements/problem description

  • The Promise. All method is used to throw an exception.

(1) All results are not available, (2) the results before the exception are available, (3). Or some of the results will work.

Thinking ๐Ÿค”

  1. I’m going to rule out the second answer, because I wrote the code, and I remember, in the promise.all method, the index index was used to ensure that the order of the results returned was the same as the order of the incoming results, so the results before the exception were not necessarily executed before the exception, so that’s the first rule out.
  2. I was struggling with answer 1 and answer 2 at that time. I was not sure whether I directly reject and quit when I met abnormality.
  • So here, I will take myself, to the previous handwritten code moved over, carefully study, and then remember!!!!!!

The actual research

// The basic implementation and method of promise are not posted here, you can check the series 1 article, here are the main points
   all(promises) {
       // All can also be chained, so we need to return a promise like then
       return new Promise((resolve, reject) = > {            
           let res = []     // An array to store all the results returned after processing
           const length = promises.length // The array length of the argument passed in
           promise.forEach((promise, index) = >{
           // Iterate over each promise object in the array, calling its then method to retrieve resolve or Reject data+ โš ๏ธ look here// โš ๏ธ the main thing to see here is!!!!! . Then returns resolve or reject,
          // So, reject exceptions should also be placed in the corresponding position according to index.
               promise.then(result= > {
               // handle a push
                   res.push(result)
               // Resolve returns when the res result array is the same length as the passed argument
               if(res.length === promiseLength) resolve(res)
                   }).catch(e= > reject(e))
               })
           })
       
       })
   }

Copy the code
  • So, according to my previous handwritten source code, let’s write a demo to see if the source code studied is the same as the actual outputยท All results are resolved, so the output is an array of results.
const a =  new Promise((resolve, reject) = > setTimeout(() = > resolve(new Error('An exception has occurred! ')), 10))
const b =  new Promise((resolve, reject) = > setTimeout(() = > resolve('111111'), 10))
const c =  new Promise((resolve, reject) = > setTimeout(() = > resolve(new Error('An exception has occurred! ')), 10))
const d =  new Promise((resolve, reject) = > setTimeout(() = > resolve('111111'), 10))


Promise.all([a, b, c, d]).then(data= > console.log(data)).catch(err= > console.log(err))

Copy the code
  • Output result:
[ Error: Abnormal happened! At the Timeout. _onTimeout (/ Users/cookie/Desktop/Leetcode basis/JS/Promise/ProseAll JS: 1:7 0) at listOnTimeout (internal/timers.js:554:17) at processTimers (internal/timers.js:497:7),'111111'Error: An exception has occurred! At the Timeout. _onTimeout (/ Users/cookie/Desktop/Leetcode basis/JS/Promise/ProseAll JS: he 0) at listOnTimeout (internal/timers.js:554:17) at processTimers (internal/timers.js:497:7),'111111'// the result of resolve is normal.Copy the code
  • If there's a reject, it's just a reject,Let’s test it out:

const a =  new Promise((resolve, reject) = > setTimeout(() = > resolve(new Error('An exception has occurred! ')), 0))

+ // There is a reject with a timer of 10
const b =  new Promise((resolve, reject) = > setTimeout(() = > reject('111111'), 10))
+ // There is also a reject with a timer of 0
const c =  new Promise((resolve, reject) = > setTimeout(() = > reject(new Error('An exception has occurred! ')), 0))
const d =  new Promise((resolve, reject) = > setTimeout(() = > resolve('111111'), 10))




Promise.all([a, b, c, d]).then(data= > console.log(data)).catch(err= > console.log(err))

Copy the code

Output result:

Error: An exception has occurred! At the Timeout. _onTimeout (/ Users/cookie/Desktop/Leetcode basis/JS/Promise/PromiseAll JS: 39) at listOnTimeout (internal/timers.js:554:17) at processTimers (internal/timers.js:497:7)Copy the code
  • So, to sum up, if all resolve is passed, the output is an array of returned results, but if a Reject is present, the first reject is returned, regardless of whether there has been any prior completion.

conclusion

Every time the research and learning, can not stop thinking, this thing will write when writing, encountered asked the question did not think, meng, so, need to keep thinking, to remember the learned. Thanks for the comment section big guy put forward the little brother's mistake, here has been corrected, thank you! ๐Ÿ™Copy the code

In promise.all, if all of the resolve output is used, the results will be returned in the specified subscript order. Therefore, if the request to be processed throws an exception, the exception is also output at the specified location. Does not affect the results returned by other requests/asynchronous processing. However, if reject is used and executed, the output is the result returned by the first reject.

Front end small white, thank you for watching, if the feeling can also hope to give a thumbs up ๐Ÿ‘. Thank you! If there are mistakes, please point out. Little brother will correct immediately!Copy the code