0

I need to perform something like:

let promise1 = getPromise1();
let promise2 = getPromise2();
let promise3 = getPromise3();

// some more code
...

result1 = await promise1;
// work with result1 in specific way

result2 = await promise2;
// work with result2 in specific way

result3 = await promise3;
// work with result3 in specific way

Since I need to perform rather unique thingson each of the results, I do not want to work with Promise.all, as itd result in some nasty if-else structure.

My question is - where does try{}catch(){} block come into place? Even if one of the promises errors, I do not want it to affect the other ones. They must finish execution regardless. Do I wrap single resultX in try{} or do I wrap let promiseX =... . Or both?

Do you have better design pattern in mind?

Thanks!

michnovka
  • 2,880
  • 3
  • 26
  • 58
  • What do you want inside `result1` if `promise1` rejects? – Ry- Jun 21 '19 at 02:57
  • If you want these to work sequentially and one fails it doesn't affect the others, then you either wrap each one in a `try/catch` or use `.then().catch()` on each one to trap errors. If you want them to work in parallel, but monitor all of them completing regardless of errors, then you want something like [`Promise.settle()`](https://stackoverflow.com/questions/36605253/es6-promise-all-error-handle-is-settle-needed/36605453#36605453) or the proposed [`Promise.allSettled()`](https://github.com/tc39/proposal-promise-allSettled) instead of `Promise.all()`. – jfriend00 Jun 21 '19 at 03:01
  • Or, if you don't need to track when they are all done and want them to run in parallel, just use `.then().catch()` on each one instead of `await`. `await` is for serializing/sequencing async activities. – jfriend00 Jun 21 '19 at 03:02
  • To be honest, we can't really answer until we understand better what you're actually trying to do with the results, whether the async operations can be run in parallel or must be sequences and what type of result you want at the end. This is where pseudo-code that doesn't show what you're reallying trying to do crashes and burns for communicating the real problem and handicaps our ability to give you the best advise. – jfriend00 Jun 21 '19 at 03:04

1 Answers1

0

Like you mentioned, if you want to separately work with different promises, you can simply wrap them up inside a try/catch block of their own.


let result1 = []; // for simplicity

try {
  result1 = await getPromise1()
} catch (error) {
  console.log(`Error occured: ${error}`);
  // fallback
  result1 = someComplicatedOperationHere();
}

You can follow a similar approach to the other n promises you have and at last you'll have a result1 .. resultN all containing something. Either the successful result, or the fallback (or however you wish to handle it).

However, note that if you use await and have several different promise calls, they will be fetched synchronously for your application. Meaning: They will (a)wait for the first promise to return and then move on.

If you do not want that, you are going to have to use Promise.all. (A classic await in for loop kinda issue.)

code
  • 2,115
  • 1
  • 22
  • 46