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?
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?
Show answerHide answer
Correct answer: D Rejected Resolved4
Explanation
Explanation:This question tests your understanding of promise chaining and error handling with catch(). The execute() function returns a Promise that immediately rejects (calls reject()). The initial promise chain starts with .then() handlers for the resolved case, but since the promise rejects, all .then() handlers before the first .catch() are skipped. The .catch() handler executes and logs 'Rejected'. After .catch(), the chain continues with .then() because a .catch() returns a new resolved promise by default (unless it throws an error). Therefore, 'Resolved4' is logged after 'Rejected'. The final output is 'Rejected' followed by 'Resolved4'.
Correct Option:
D. Rejected Resolved4 – Correct. Let's trace the execution:
execute() returns a Promise that rejects immediately.
The first .then() on line 07 is skipped because the promise is rejected.
The second .then() on line 08 is skipped.
The third .then() on line 09 is skipped.
The .catch() on line 10 catches the rejection and logs 'Rejected'.
The .catch() returns a resolved promise by default (no error thrown), so the chain continues.
The final .then() on line 11 executes and logs 'Resolved4'.
Final output: Rejected Resolved4.
Incorrect Options:
A. Resolved1 Resolved2 Resolved3 Rejected Resolved4 –
Incorrect. The initial promise rejects, so none of the .then() handlers before the .catch() execute. This option incorrectly assumes the .then() handlers run before the .catch(). They are skipped entirely.
B. Rejected –
Incorrect. This option ignores the final .then() after the .catch(). Once the .catch() handles the rejection, it returns a new resolved promise, allowing the subsequent .then() to execute. The chain does not stop after .catch() unless an error is thrown or the chain is intentionally terminated.
C. Resolved1 Resolved2 Resolved3 Resolved4 –
Incorrect. This option completely ignores the rejection. It would only be correct if the promise resolved instead of rejected. Since the promise rejects, the .catch() executes and the preceding .then() handlers are skipped.
Reference:
MDN Web Docs – Promise.prototype.catch() method
MDN Web Docs – Promise chaining and error handling
MDN Web Docs – Returning from catch and continuing chains
Salesforce Trailhead – JavaScript Essentials: Asynchronous Programming and Promises