I have some problems for handling multiple failures in parallel. How to handle deviation in async functions when we "wait in parallel".
Example:
function in_2_sec(number) {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('Error ' + number);
}, 2000);
})
}
async function f1() {
try {
let a = in_2_sec(3);
let b = in_2_sec(30);
return await a + await b;
} catch(err) {
console.log('Error', err);
return false;
}
}
async function f2() {
try {
let a = await Promise.all([in_2_sec(3), in_2_sec(30)]);
return a[0] + a[1];
} catch(err) {
console.log('Error', err);
return false;
}
}
f1()
create UnhandledPromiseRejectionWarning
in node because the second deviation (b) is not processed.
f2()
works fine, Promise.all()
performs the trick, but how to do it f2()
only with async / wait syntax, without Promise.all()
?
source
share