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?