Catching All Failure Promises In Async Function In JavaScript

I had a problem with catching all errors in case of multiple errors reject rejection promises after waiting in async function (javaScript - node v8.4.0).

Make a link to the following javaScript:

For reference, the functions timeoutOne () and timeoutTwo () simply return their own promise, which resolves the value after a timeout of 1 and 2 seconds, respectively, or rejects 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 a promise that returns first logs the error. I know that in some promise libraries there is a way to register all errors (for example, the "install" method in bluebird), but I'm not sure if there is an analogue of this method in native promises?

, promises , asyncParallel() , . , try/catch , ?

, promises. , , Promise.all , async , node.

, try/catch ? Promise.all , , ?

+4
1

promises , asyncParallel() , .

- timeoutTwo(), (, await). await res2 - await res1.

( , ", ", , ).

, async try/catch, ?

, .

Promise.all , , ?

, . promises , Promise.all. await - .then().

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

, , . , (, "" bluebird), , native promises?

, . settle , , then, :

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)
  }
}
+3

Source: https://habr.com/ru/post/1687382/


All Articles