javascript - Resolves propagating up multiple calling async functions -
i've been trying reject
s of asynchronous functions bubble callers, it's not working reason. here's tested example code:
"use strict"; class test { constructor() { this.do1(); } async do1() { try { this.do2(); } catch(reason) { console.error(reason); } } async do2() { for(let = 0; < 10; i++) { await this.do3(); console.log(`completed ${i}`); } console.log("finished do1"); } async do3() { return new promise((resolve, reject) => { settimeout(() => { if(math.random() < 0.3) reject('###rejected'); else resolve("###success"); }, 1000); }); } } export default test;
chrome gives me every time: unhandled promise rejection ###rejected
.
any idea why happening? i'd able handle thrown errors higher level do2()
(the above example works fine if try/catch in do2()
, wraps await this.do3();
). thanks!
edit: bit more explicit, if take try/catch out of do1()
, put in do2()
follows, works fine:
async do2() { try { for(let = 0; < 10; i++) { await this.do3(); console.log(`completed ${i}`); } console.log("finished do1"); } catch(reason) { console.error(reason); } }
async do1() { try { await this.do2(); } catch(reason) { console.error(reason); } }
do2
asynchronous function. , call without await
. so, when completes there's no try-catch clauses around it.
see this question , this article more details.
Comments
Post a Comment