Sample questions
Question 1Easy
Which are the three states of a JavaScript Promise?
- waiting, running, completed
- pending, fulfilled, rejected
- created, resolved, cancelled
- started, succeeded, errored
Answer: B. pending, fulfilled, rejected
Explanation
A promise starts as pending and settles once, either fulfilled with a value or rejected with a reason.
Step-by-step solution
- A Promise represents the eventual result of an asynchronous operation.
- It starts in the 'pending' state, meaning the operation hasn't finished yet.
- It then settles exactly once, moving either to 'fulfilled' (success, with a resulting value) or 'rejected' (failure, with a reason) - and can never change state again after that.
Why the other options are wrong
- A. "waiting, running, completed" is not the actual terminology JavaScript Promises use for their states.
- C. "created, resolved, cancelled" doesn't match Promise terminology either; notably, Promises have no built-in 'cancelled' state.
- D. "started, succeeded, errored" is close in spirit but doesn't use the actual specified Promise state names.
Tip: Memorise the three exact Promise states: pending, fulfilled, rejected - these specific words are what documentation and APIs (like Promise.allSettled) actually use.
Question 2Easy
What is the output?
Promise.resolve(1)
.then(v => v + 1)
.then(v => console.log(v));
- 1
- undefined
- Promise { 2 }
- 2
Answer: D. 2
Explanation
The first then returns 2, which is passed to the second then and logged.
Step-by-step solution
- resolve(1) -> v = 1.
- First then returns 2.
- Second then logs 2.
Why the other options are wrong
- A. Each then receives the previous return value.
- B. The first then returns a value.
- C. The value is unwrapped before the second then.
Question 3Easy
What is the output?
Promise.reject(new Error('x'))
.catch(e => console.log('caught', e.message))
.then(() => console.log('after'));
- caught, x
- after
- caught, x, after
- after, caught, x
Answer: C. caught, x, after
Explanation
catch handles the rejection and returns normally, so the chain continues with "after".
Step-by-step solution
- The rejection goes to catch.
- catch returns undefined, so the chain resolves.
- Next then prints after.
Why the other options are wrong
- A. The chain continues after a handled rejection.
- B. The catch handler runs first.
- D. catch runs before the later then.
Question 4Easy
What are the three states of a Promise?
- Waiting, running, finished
- Created, resolved, closed
- Pending, fulfilled, rejected
- Pending, success, error
Answer: C. Pending, fulfilled, rejected
Explanation
A promise starts pending and settles once, either fulfilled with a value or rejected with a reason.
Step-by-step solution
- Starts pending.
- Settles to fulfilled or rejected, once.
Why the other options are wrong
- A. These are not Promise state names.
- B. There is no closed state.
- D. The states are fulfilled and rejected.
Question 5Medium
What is the output?
Promise.race([
new Promise(r => setTimeout(() => r('slow'), 20)),
new Promise(r => setTimeout(() => r('fast'), 5))
]).then(console.log);
- slow
- ['slow', 'fast']
- fast
- fast, slow
Answer: C. fast
Explanation
Promise.race settles with whichever promise settles first, here the 5 ms one.
Step-by-step solution
- Two timers: 20 ms and 5 ms.
- The faster settles first.
- race resolves with "fast".
Why the other options are wrong
- A. race settles with the first promise to settle.
- B. race returns a single value.
- D. race resolves only once.
Question 6Medium
What is the output?
Promise.all([1, Promise.resolve(2), 3]).then(v => console.log(v.join()));
- 1,2,3
- 1,[object Promise],3
- 2
- 3
Answer: A. 1,2,3
Explanation
Promise.all accepts non-promise values too (they are treated as already resolved) and resolves with all results in input order.
Step-by-step solution
Promise.all() waits for ALL the promises (or values) in its input array to settle, then resolves with an array of all their results, in the same order as the input.- It also accepts plain, non-promise values mixed in with actual promises - these are simply treated as already-resolved values.
- Here,
[1, Promise.resolve(2), 3] resolves to [1, 2, 3], and .join() turns that array into the comma-separated string "1,2,3".
Why the other options are wrong
- B. "1,[object Promise],3" would be the result only if Promise.all failed to 'unwrap' the Promise.resolve(2) entry, but Promise.all specifically resolves each entry to its underlying value before combining them.
- C. 2 alone would suggest only the Promise.resolve(2) entry mattered, but Promise.all preserves and returns ALL entries, in their original order, not just the promise-typed ones.
- D. 3 alone similarly ignores that Promise.all returns the full array of all results, not just the last value.
Tip: Promise.all() preserves input order in its results, regardless of which promises actually settle first.
Question 7Medium
What is the output?
async function f() {
try {
await Promise.reject(new Error('bad'));
} catch (e) {
return 'handled ' + e.message;
} finally {
console.log('cleanup');
}
}
f().then(console.log);
- handled bad, cleanup
- cleanup
- cleanup, bad
- cleanup, handled bad
Answer: D. cleanup, handled bad
Explanation
await throws on rejection, catch returns a string, finally logs cleanup first, then the promise resolves and then logs.
Step-by-step solution
- await throws the error.
- catch returns the string.
- finally prints cleanup.
- then logs the returned value.
Why the other options are wrong
- A. finally runs before the promise resolves.
- B. The returned string is logged too.
- C. The catch block returns a string, not the message.
Question 8Medium
What is the output?
Promise.resolve(1)
.then(v => { throw new Error('boom'); })
.then(() => console.log('A'))
.catch(() => console.log('B'))
.then(() => console.log('C'));
- A, B, C
- B
- B, C
- A, C
Answer: C. B, C
Explanation
The throw rejects the chain, skipping the then that prints A; catch prints B and resolves, so C prints.
Step-by-step solution
- Throw rejects the promise.
- Then A is skipped.
- catch prints B, recovering.
- Then C prints.
Why the other options are wrong
- A. A is skipped because a rejection bypasses plain then handlers.
- B. The final then still runs after a handled error.
- D. The rejection reaches catch.
Question 9Hard
What is the output?
new Promise((resolve, reject) => {
resolve('first');
reject(new Error('second'));
resolve('third');
}).then(console.log, e => console.log('err'));
- err
- first
- third
- first, err
Answer: B. first
Explanation
A promise can settle only once; the later reject and resolve calls are ignored.
Step-by-step solution
- resolve("first") settles the promise.
- Later calls are ignored.
- then logs "first".
Why the other options are wrong
- A. Only the first settle call has any effect.
- C. Later calls are ignored.
- D. A promise settles once.
Question 10Hard
You need the results of several independent requests, and one failure must not discard the others. Which combinator fits best?
- Promise.allSettled
- Promise.race
- Promise.any
- Promise.all
Answer: A. Promise.allSettled
Explanation
Promise.allSettled waits for every promise and reports each outcome as {status, value} or {status, reason}. Promise.all rejects as soon as any one rejects.
Step-by-step solution
- You need the outcome of EVERY promise, successful or not, without one failure wiping out the rest of the results.
Promise.all() would reject immediately as soon as any single promise rejects, discarding the results of the others - not what's wanted here.Promise.allSettled() instead waits for every promise to settle (whether fulfilled or rejected) and gives you an array describing each individual outcome.
Why the other options are wrong
- B. Promise.race() settles as soon as the FIRST promise settles (ignoring all the rest entirely), which doesn't give you every result.
- C. Promise.any() resolves with the first SUCCESS and ignores failures, but it still doesn't give you every individual promise's own outcome.
- D. Promise.all() rejects as a whole the moment any single promise rejects, discarding the other, possibly successful, results - the opposite of what's needed here.
Tip: Need 'all results, failures included, nothing discarded'? Use Promise.allSettled(), not Promise.all().
Practise all 21 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →