constmain = async () => { for (let i = 0; i < 5; i++) { try { promise1(i); promise2(); promise3(); } catch(err) { console.log(`err =>${err}`); } } }
main();
// 返回
0 3 1 3 2 3 3 3 4 3 (node:3226) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: console is not a function
同步执行
如果使用了async await, main 函数变成这样
1 2 3 4 5 6 7 8 9 10 11
constmain = async () => { for (let i = 0; i < 5; i++) { try { awaitpromise1(i); awaitpromise2(); awaitpromise3(i); } catch(err) { console.log(`err =>${err}`); } } }
1 2 3 4 5 6 7 8 9 10
0 err =>TypeError: console is not a function 1 err =>TypeError: console is not a function 2 err =>TypeError: console is not a function 3 err =>TypeError: console is not a function 4 err =>TypeError: console is not a function
0 TypeError: console is not a function at Promise (/Users/bhb/code/testTryCatch.js:10:9) at newPromise (<anonymous>) at promise2 (/Users/bhb/code/testTryCatch.js:9:12) at main (/Users/bhb/code/testTryCatch.js:30:19) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:678:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3 3 1 TypeError: console is not a function at Promise (/Users/bhb/code/testTryCatch.js:10:9) at newPromise (<anonymous>) at promise2 (/Users/bhb/code/testTryCatch.js:9:12) at main (/Users/bhb/code/testTryCatch.js:30:19) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:678:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3 3 2 TypeError: console is not a function