简体   繁体   中英

Typescript Handling promise errors

In a controller I have multiple calls to some methods that return a promise.

I'm going to use the await/async statement and I have something like this:

try {
  let foo = await myFirstMethod();
  let bar = await mySecondMethod();
}catch(e => {
  // which method fails between the both?
});

Yes, I know that I could split the call in two separate try/catch statement, but I have to handle also this scenario and in addiction I'd like to understand which is the better way to have a specific type of error response for each methods.

Any helps or suggestions are appreciated and welcome.

Thanks

What you're looking for isPromise.allSettled

let promises = [myFirstMethod() ,mySecondMethod()];

let allResults = Promise.allSettled(promises).then((results) => {
    // results is an array of objects representing the promises' final state
    // result.status is either "fulfilled" or "rejected"

   results.forEach(result => {
       console.log(result.status);
   });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM