0

I am calling 2 functions using Promise.all function, where I am passing an odd number and an even number. Based on the number I am deciding the success or failure.

const time11=(t) =>{
  return new Promise((resolve,reject) =>{
   if(t%2==0){
     resolve(t)
   }else{
     reject(t)
   }
  })
}

// Promise.all
Promise.all([time11(101), time11(1210)])
 .then(result => console.log('success',result))
 .catch(error=> console.log('error',error))

I expect the output is success 1210 and error 101, but the actual output is error 101.

kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
praveen
  • 11
  • 5
  • 2
    `[time11(101), time11(1210)].forEach(p => p .then(result => console.log('success',result)) .catch(error=> console.log('error',error)) )` – Patrick Roberts Oct 08 '19 at 11:59
  • `Promise.all` only resolves if ALL passed promises resolve, it rejects everything as you see. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all – gcasar Oct 08 '19 at 12:00
  • Check out `Promise.allSettled()` – marzelin Oct 08 '19 at 15:32
  • Possible duplicate of [Why does JavaScript's \`Promise.all\` not run all promises in failure conditions?](https://stackoverflow.com/questions/42304394/why-does-javascripts-promise-all-not-run-all-promises-in-failure-conditions) – Heretic Monkey Oct 08 '19 at 15:35
  • Or [Wait until all promises complete even if some rejected](https://stackoverflow.com/q/31424561/215552) – Heretic Monkey Oct 08 '19 at 15:36

2 Answers2

1

Promise#allSettled

The Promise.allSettled() method returns a promise that resolves after all of the given promises have either resolved or rejected, with an array of objects that each describes the outcome of each promise.

const time11=(t) =>{
  return new Promise((resolve,reject) =>{
   if(t%2==0){
     resolve(t)
   }else{
     reject(t)
   }
  })
}

Promise
.allSettled([time11(101), time11(1210)])
.then(result =>console.log(result));

However, only supported in a few browsers at the moment. Above snippet will only work with latest version of chrome.

Output, will look something like so:

[
  {
    "status": "rejected",
    "reason": 101
  },
  {
    "status": "fulfilled",
    "value": 1210
  }
]

Here is to separate the errors from the successes.

const time11=(t) =>{
  return new Promise((resolve,reject) =>{
   if(t%2==0){
     resolve(t)
   }else{
     reject(t)
   }
  })
}

Promise
.allSettled([time11(101), time11(1210)])
.then(result =>{
  const [success, errors] = result.reduce(([success, errors],cur)=>{
    if(cur.status === 'fulfilled'){
      success.push(cur);
    } else {
      errors.push(cur);
    }
    return [success, errors];
  }, [[],[]]);
  
  console.log('errors', errors);
  console.log('success', success);
});
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • Hi kemicofa, Thanks for the suggestion but unfortuntely Promise.allSettled function is not available in node js. – praveen Oct 16 '19 at 06:12
0

Promise.all is all or nothing. It resolves once all promises in the array resolve, or reject as soon as one of them rejects. In other words, it either resolves with an array of all resolved values, or rejects with a single error.

Some libraries have something called Promise.when, which I understand would instead wait for all promises in the array to either resolve or reject, but I'm not familiar with it, and it's not in ES6.

In this case, you can resolve all cases, but you must manage different based in the response at the then chain.

const time11=(t) =>{
  return new Promise((resolve,reject) =>{
   if(t%2==0){
     resolve(true) // Changed to boolean
   }else{
     resolve(false)  // Resolved and Boolean
   }
  })
}

// Promise.all
Promise.all([time11(101), time11(1210)])
 .then(isEven => console.log('isEven',isEven))
luispa
  • 39
  • 4