Salesforce Certified JavaScript Developer free question 8 / 10 · Domain 5: Asynchronous Programming
One of two inputs to Promise.all rejects. const slowOk = new Promise((resolve) => setTimeout(() => resolve('A'), 10)); const bad = Promise.reject(new Error('B failed')); Promise.all([slowOk, bad]) .then((values) => console.log('values', values)) .catch((err) => console.log('error', err.message)); What is logged?
- The console stays empty, because the rejection is not handled until another input has settled
- values A, because Promise.all skips rejected promises and returns the fulfilled values
- values A,undefined, because a rejected input becomes undefined in the result array
- error B failed, because Promise.all rejects with the reason of the first rejection
Show the answer and explanation
Answer: D
Why this is correct
Promise.all fulfills with an array of values only when all inputs fulfill. It rejects as soon as any input rejects, using the first rejection reason, so the then handler is skipped and the catch handler logs the message of the rejected promise.
Why the other options are wrong
"values A, because Promise.all skips rejected promises and returns the fulfilled values" … The returned promise fulfills when all inputs fulfill. When one input rejects, it rejects with that reason and the then handler does not run.
"values A,undefined, because a rejected input becomes undefined in the result array" … A rejection does not become an array entry. The promise returned by Promise.all rejects, and the result array is never produced.
"The console stays empty, because the rejection is not handled until another input has settled" … Promise.all rejects as soon as one input rejects, without waiting for the pending input, and the catch handler receives the reason.
Key points
Use Promise.all when the tasks depend on each other, and Promise.allSettled when you need every outcome.