最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

error handling - Catching All Promise Rejections in an Async Function in JavaScript - Stack Overflow

programmeradmin3浏览0评论

I've ran into an issue with catching all the errors when multiple promises throw rejection errors after being awaited in an async function (javaScript - node v8.4.0).

Make reference to the following javaScript:

For reference, the functions timeoutOne() and timeoutTwo() simply return a native promise that resolves a value after a 1 and 2 second timeout respectively, or reject with an error if I set "deviousState" to true.

let deviousState = true;

async function asyncParallel() {
  try {
    let res1 = timeoutOne();
    let res2 = timeoutTwo();
    console.log(`All done with ${await res1} ${await res2}`)
  }
  catch(err) {
    console.log(err)
  }
}
asyncParallel();

let pAll = Promise.all([timeoutOne(), timeoutTwo()]);
pAll.then((val) => {
  console.log(`All done with ${val[0]} ${val[1]}`)
}).catch(console.log);

In both cases only the promise that returns first logs an error. I know that in some promise libraries there is a way to log all the errors (for example the "settle" method in bluebird), however, I'm not sure if there is an analogue to this method in native promises?

Also, if both promises reject, then asyncParallel() logs an uncaught error with the promise that rejects last. Is that because there is no built in mechanism for async function's try / catch blocks to catch multiple rejections in this way?

Everything works the same in both instances if the promises resolve. Its just that when both reject, the Promise.all handles the errors, and the async function version states that one of the unhandled promise errors will crash the process in future versions of node.

Is there anyway for try / catch to handle this type of error correctly? Or do I still need to use a Promise.all inside async functions to make sure that the errors are handled correctly?

I've ran into an issue with catching all the errors when multiple promises throw rejection errors after being awaited in an async function (javaScript - node v8.4.0).

Make reference to the following javaScript:

For reference, the functions timeoutOne() and timeoutTwo() simply return a native promise that resolves a value after a 1 and 2 second timeout respectively, or reject with an error if I set "deviousState" to true.

let deviousState = true;

async function asyncParallel() {
  try {
    let res1 = timeoutOne();
    let res2 = timeoutTwo();
    console.log(`All done with ${await res1} ${await res2}`)
  }
  catch(err) {
    console.log(err)
  }
}
asyncParallel();

let pAll = Promise.all([timeoutOne(), timeoutTwo()]);
pAll.then((val) => {
  console.log(`All done with ${val[0]} ${val[1]}`)
}).catch(console.log);

In both cases only the promise that returns first logs an error. I know that in some promise libraries there is a way to log all the errors (for example the "settle" method in bluebird), however, I'm not sure if there is an analogue to this method in native promises?

Also, if both promises reject, then asyncParallel() logs an uncaught error with the promise that rejects last. Is that because there is no built in mechanism for async function's try / catch blocks to catch multiple rejections in this way?

Everything works the same in both instances if the promises resolve. Its just that when both reject, the Promise.all handles the errors, and the async function version states that one of the unhandled promise errors will crash the process in future versions of node.

Is there anyway for try / catch to handle this type of error correctly? Or do I still need to use a Promise.all inside async functions to make sure that the errors are handled correctly?

Share Improve this question asked Oct 12, 2017 at 14:47 MFaveMFave 1,0742 gold badges9 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

If both promises reject, then asyncParallel() logs an uncaught error with the promise that rejects last.

Yes - you created the timeoutTwo() promise but never handled its errors (like using it in an await). The await res2 was never executed due to the exception in await res1.

(Notice it's not "the promise that rejects last", but always the promise that is awaited second).

Is that because there is no built in mechanism for async function's try / catch blocks to catch multiple rejections in this way?

In sequential code, there cannot be multiple exceptions, so ing up with extra syntax to deal with them would be hard.

Do I still need to use a Promise.all inside async functions to make sure that the errors are handled correctly?

Yes, exactly that. If you want to wait for multiple promises in parallel, you should always use Promise.all. The await keyword is just sugar for the following .then() call.

You should write

async function asyncParallel() {
  try {
    let [val1, val2] = await Promise.all([timeoutOne(), timeoutTwo()]);
    console.log(`All done with ${val1} ${val2}`)
  } catch(err) {
    console.log(err)
  }
}

In both cases only the promise that returns first logs an error. I know that in some promise libraries there is a way to log all the errors (for example the "settle" method in bluebird), however, I'm not sure if there is an analogue to this method in native promises?

No, there's not. The characteristics of settle are trivial to implement yourself using then, with any values you desire:

async function asyncParallel() {
  try {
    let [stat1, stat2] = await Promise.all([
        timeoutOne().then(() => "one fulfilled", () => "one rejected"), 
        timeoutTwo().then(() => "two fulfilled", () => "two rejected")
    ]);
    console.log(`All settled with ${stat1} ${stat2}`)
  } catch(err) {
    console.log(err)
  }
}
发布评论

评论列表(0)

  1. 暂无评论