Refer to the code:
01 function execute() {
02 return new Promise((resolve, reject) => reject());
03 }
04 let promise = execute();
05
06 promise
07 .then(() => console.log('Resolved1'))
08 .then(() => console.log('Resolved2'))
09 .then(() => console.log('Resolved3'))
10 .catch(() => console.log('Rejected'))
11 .then(() => console.log('Resolved4'));
What is the result when the Promise in the execute function is rejected?
execute() returns a Promise that immediately calls reject().
So promise starts in a rejected state.
When a Promise is rejected and you chain .then() calls without rejection handlers, all those .then() callbacks are skipped until a .catch() is encountered:
promise
.then(...) // skipped
.then(...) // skipped
.then(...) // skipped
.catch(...) // executed
.then(...); // executed after catch
Execution:
.then(() => console.log('Resolved1')) is skipped.
.then(() => console.log('Resolved2')) is skipped.
.then(() => console.log('Resolved3')) is skipped.
.catch(() => console.log('Rejected')) runs and logs Rejected.
The .catch() returns a resolved Promise (no explicit return, so undefined), so the next .then() runs:
.then(() => console.log('Resolved4')) logs Resolved4.
Final output:
Rejected
Resolved4
This matches option D.
Jade
5 days agoHuey
10 days agoPrecious
15 days agoDeane
20 days agoDevorah
25 days ago