Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Salesforce JS-Dev-101 Exam - Topic 5 Question 11 Discussion

Refer to the code:01 function execute() {02 return new Promise((resolve, reject) => reject());03 }04 let promise = execute();0506 promise07 .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?
D) Rejected Resolved4
A) Resolved1 Resolved2 Resolved3 Rejected Resolved4
B) Rejected
C) Resolved1 Resolved2 Resolved3 Resolved4

Salesforce JS-Dev-101 Exam - Topic 5 Question 11 Discussion

Actual exam question for Salesforce's JS-Dev-101 exam
Question #: 11
Topic #: 5
[All JS-Dev-101 Questions]

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?

Show Suggested Answer Hide Answer
Suggested Answer: D

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.


Contribute your Thoughts:

0/2000 characters
Jade
5 days ago
The promise is rejected right away.
upvoted 0 times
...
Huey
10 days ago
I feel like the answer might be D, because after the catch, it could still run the last 'then' for 'Resolved4'. But I could be wrong!
upvoted 0 times
...
Precious
15 days ago
This reminds me of a practice question where the promise was resolved, and all the 'then' statements executed. But here, since it's rejected, I guess it skips to the catch.
upvoted 0 times
...
Deane
20 days ago
I'm not entirely sure, but I remember something about how the catch block handles rejections. Maybe it just outputs 'Rejected'?
upvoted 0 times
...
Devorah
25 days ago
I think the promise gets rejected right away, so it won't execute any of the 'Resolved' logs before the catch.
upvoted 0 times
...

Save Cancel