4

I currently have this code:

const promise1 = aPromiseCall().then(r => logger(r)).catch(e => logger(e));

const promise2 = anotherPromiseCall().then(r => logger(r)).catch(e => logger(e));

and in an async function I do:

const results = Promise.all([promise1, promise2]);

I do it this way because I want to ensure that if promise1 fails, I still can do promise2. However, I don't know if that's the best way to accomplish this. Should I be doing those then, catch in each promise or is there a more idiomatic way of doing this?

At the same time I want to guarantee that ALL the promises are resolved/rejected before I continue the execution of my code, and that's why I put them in a Promise.all.

Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
  • FYI you're missing an extra `)` at the end of the first line of code. – AskYous Mar 30 '19 at 18:08
  • Shouldn't be doing the `then()` without a `return` if you want the results of each promise passed to `then()` of `Promise.all`. You have oversimplified your use case so it's not clear what expectations are – charlietfl Mar 30 '19 at 18:13
  • @charlietfl I'm just logging the results/errors. Do I need a return? I don't really care about doing a then in Promise.all, I just use Promise.all to make sure all the promises are done. Because after Promise.all I do something else, and all those proimises must be done by then. – Hommer Smith Mar 30 '19 at 18:23
  • 1
    OK....that's what I meant by expectations. If you don't need the results in Promise.all then what you are doing should be basically ok. A `catch()` that doesn't throw the error passed to it returns a new promise down the chain and thus will resolve in Promise.all – charlietfl Mar 30 '19 at 18:24
  • Cool, @charlietfl can you add an answer with your comment, I think it's solving my original question. – Hommer Smith Mar 30 '19 at 19:09
  • "*I want to ensure that if promise1 fails, I still can do promise2*" - I'm not sure what you mean by that. Even when you just do `Promise.all([aPromiseCall(), anotherPromiseCall()])`, both functions are called and run their tasks regardless whether one of them rejects the returned promise. – Bergi Mar 30 '19 at 19:51

2 Answers2

2

Please refer Handling errors in Promise.all

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.

You can write some code like-

Promise.all([promise1, promise2]).then(r => console.log(r)).catch(e => console.log(e));

This could be the solution to the problem refer to https://stackoverflow.com/a/31524969/3270651-

let a = new Promise((res, rej) => res('Resolved!')),
    b = new Promise((res, rej) => rej('Rejected!')),
    c = a.catch(e => { console.log('"a" failed.'); return e; }),
    d = b.catch(e => { console.log('"b" failed.'); return e; });

Promise.all([c, d])
  .then(result => console.log('Then', result)) // Then ["Resolved!", "Rejected!"]
  .catch(err => console.log('Catch', err));

Promise.all([a.catch(e => e), b.catch(e => e)])
  .then(result => console.log('Then', result)) // Then ["Resolved!", "Rejected!"]
  .catch(err => console.log('Catch', err));
D Mishra
  • 1,518
  • 1
  • 13
  • 17
0

From the MDN:

Promise.all is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, then Promise.all will reject immediately.

In order to handle possible rejections:

const promise1 = aPromiseCall();        
const promise2 = anotherPromiseCall();


// In your async function:

const results = await Promise.all([
  promise1.catch(error => error),
  promise2.catch(error => error),
]);

console.log(results[0])
console.log(results[1])

var promise1 = new Promise((resolve, reject) => { 
  setTimeout(() => resolve('resolved'), 2000); 
}); 

var promise2 = new Promise((resolve, reject) => {
  setTimeout(() => reject('rejected'), 1000); 
});

(async function() {
  const results = await Promise.all([
    promise1.catch(error => error),
    promise2.catch(error => error),
  ]);

  console.log(results[0]);
  console.log(results[1]);
})();
Fraction
  • 11,668
  • 5
  • 28
  • 48