4 ms·
I don't get a warning for this code: async function foo() { try { // promise1 will resolve after one second let promise1 =
by petilon 5y ago
I don't get a warning for this code:
async function foo() {
try {
// promise1 will resolve after one second
let promise1 = new Promise(resolve => setTimeout(resolve, 1000))
// promise2 will wait for the next even loop tick to call its error handler
let promise2 = Promise.reject('something bad')
// At this point, you should await promise2, or set an error handler using .catch
// But you can do some sync stuff if you want to:
someExpensiveCPUStuff()
// No warning in Node.js
let result2 = await promise2;
let result1 = await promise1;
}
catch (ex) {
console.log(ex)
}
}
- WolfOliver 5y agoso console.log(ex) is never executed?
- WolfOliver 5y agoyou have to execute it like this: await foo(); Then you will see the error
- fathyb 5y agoYou should if you await promise1 first: let result1 = await promise1; let result2 = await promise2;
- petilon 5y agoYeah, but the goal is to kick off two async calls in parallel. The order in which you await is unimportant for that goal. If awaiting promise2 first solves the problem then that's the solution!