4 ms·
> will wait for the next even loop tick to call its error handler are you sure about that? Promise.reject is synchronous and won't wait for the next tick it j
by mfbx9da4 5y ago
> will wait for the next even loop tick to call its error handler
are you sure about that?
Promise.reject is synchronous and won't wait for the next tick it just returns a rejected promise
try/catch can be used fine
async function f() {
throw new Error("asdf");
}
let result1 = f();
let result2 = f();
try {
await result1;
} catch (e) {
console.log("caught the error");
}
try {
await result2;
} catch (e) {
console.log("caught the error");
}
- fathyb 5y agoThe error handler won't be called until the next tick: Promise .reject('error') .catch(error => console.log('Error handler called:', error)) console.log('After reject') This will print: After reject Error handler called: error
- lhorie 5y agoI think the point is that try/catch in async code is tied to the presence of await. This will run happily without falling into the catch clause: const err = async () => { throw new Error('async error') } try { const rejected = Promise.reject(); const errored = err(); console.log('ok!'); // in real code, this might have returned a deferred to be handled elsewhere instead } catch (e) { console.log('not ok!'); // never gets here }