promise.allsettled polyfill node 1

promise.allsettled polyfill node

Promise.allSettled = function (promises) {
  let mappedPromises = promises.map((p) => {
    return p      
      .then((value) => {        
        return {          
          status: 'fulfilled',         
          value,        
        };      
      })      
      .catch((reason) => {        
        return {          
          status: 'rejected',        
          reason,        
        };      
      });  
    });  
  return Promise.all(mappedPromises);};

Here is what the above code is Doing:
1. We’re mapping over the promises array and returning a new array of promises.
2. For each promise, we’re returning a new promise that will either resolve or reject.
3. If the promise resolves, we return an object with a status of fulfilled and the value.
4. If the promise rejects, we return an object with a status of rejected and the reason.
5. Finally, we return a promise that resolves when all the promises in the mappedPromises array have resolved.

Similar Posts