The requirement terminates execution and jumps out of the promise if a step in the promise fails.
Key points throw, catch
According to the principle of Promise, the function passed in by promise will be fully executed. Resolve and Reject only save the result of execution as the parameter values required by onResolve and onReject. The onResolve and onReject methods registered with the THEN function will continue to be executed. So the reject code still executes, and throwing aborts execution of the function, giving execution back. So 5 is not going to print
var a = function () {
return new Promise((resolve, reject) = > {
reject(1);
console.log(2);
}).catch((e) = > {
console.log(e);
throw 3;
});
};
var b = async function () {
await a().catch((e) = > {
console.log(e);
throw 4;
});
console.log(5);
};
(async() = > {await b().catch((e) = > {
console.log(e); }); }) ();/ / 2
/ / 1
/ / 3
/ / 4
Copy the code