Sample questions
Question 1Easy
What is the output?
const wait = (ms, v) => new Promise(r => setTimeout(() => r(v), ms));
Promise.race([wait(50, 'slow'), wait(10, 'fast')]).then(console.log);
- slow
- fast slow
- fast
- [ 'slow', 'fast' ]
Answer: C. fast
Explanation
Promise.race settles with the first promise to settle, which is the 10 ms one.
Step-by-step solution
- wait(50, 'slow') and wait(10, 'fast') start at the same time.
- The 10 ms timer finishes first, so the 'fast' promise resolves first.
- Promise.race settles with the first result, so it logs 'fast'. The correct answer is C (fast).
Why the other options are wrong
- A. The 50 ms timer finishes later, so 'slow' does not win.
- B. Race gives one value, not both.
- D. Race does not collect an array; that is Promise.all.
Tip: Promise.race settles with whichever promise settles first, win or lose.
Question 2Easy
What does util.promisify(fn) do?
- Runs fn on a worker thread
- Makes a sync function asynchronous
- Caches fn's results
- Wraps an error-first callback API in a Promise version
Answer: D. Wraps an error-first callback API in a Promise version
Explanation
The returned function returns a Promise that rejects with err or resolves with the result instead of taking a callback.
Step-by-step solution
- promisify turns an error-first callback function into one returning a Promise.
Why the other options are wrong
- A. It does not use threads.
- B. It does not make sync async.
- C. It doesn't cache.
Tip: callback -> promise.
Question 3Easy
What does this print?
async function f() { return 5; }
console.log(f() instanceof Promise);
- false
- 5
- undefined
- true
Answer: D. true
Explanation
An async function always returns a Promise; the returned value 5 becomes its fulfilment value.
Step-by-step solution
- Any async function returns a Promise, even if it returns a plain value like 5.
- f() therefore returns a Promise object.
- Instanceof Promise is true, so it prints true. The correct answer is D (true).
Why the other options are wrong
- A. f() is a Promise, so the check is not false.
- B. f() returns a Promise, not the plain value 5; the 5 is only the resolved value.
- C. The function returns a promise, so the result is never undefined here.
Tip: Calling an async function always gives a Promise; the return value is what the promise resolves to.
Question 4Easy
Which approach best flattens deeply nested callbacks ("callback hell")?
- Add more setTimeout calls
- Promisify the APIs and use async/await
- Use synchronous fs functions everywhere
- Put all the code into one large function
Answer: B. Promisify the APIs and use async/await
Explanation
async/await lets sequential asynchronous steps read top to bottom, with try/catch for errors.
Step-by-step solution
- Promisify callback APIs.
- Use async/await to read top to bottom.
Why the other options are wrong
- A. More timers do not help.
- C. Sync calls block.
- D. One big function is worse.
Tip: Promisify + await.
Question 5Medium
What is the output?
const p = [Promise.resolve(1), Promise.reject(new Error('x')), Promise.resolve(3)];
Promise.allSettled(p).then(r => console.log(r.map(x => x.status).join(',')));
- fulfilled,fulfilled
- fulfilled,rejected,fulfilled
- rejected
- 1,x,3
Answer: B. fulfilled,rejected,fulfilled
Explanation
allSettled reports one result per input in input order, each with a status of "fulfilled" or "rejected".
Step-by-step solution
- Promise.allSettled runs for each promise in p in order.
- First promise is fulfilled, so status is 'fulfilled'.
- Second promise is rejected, so status is 'rejected'.
- Third promise is fulfilled, so status is 'fulfilled'.
- Result is ['fulfilled','rejected','fulfilled'].
- Joined by commas gives 'fulfilled,rejected,fulfilled'.
Why the other options are wrong
- A. The rejected second input is still reported, so the output has three entries, not two.
- C. allSettled never rejects, so the result is not a single rejected value.
- D. The output is statuses joined by commas, not the numbers or the error message.
Tip: Remember Promise.allSettled returns one result per promise in input order.
Question 6Medium
What does this print?
const { setTimeout: sleep } = require('timers/promises');
async function main() {
console.log('a');
await sleep(10);
console.log('b');
}
main();
console.log('c');
- a b c
- c a b
- a c
- a c b
Answer: D. a c b
Explanation
timers/promises exports a promise-based setTimeout. main() prints a, then pauses at await, so the caller prints c; b follows after 10 ms.
Step-by-step solution
- main() starts by logging 'a' to console.
- Await sleep(10) pauses execution until 10 ms pass.
- console.log('c') runs immediately after main() is called.
- sleep(10) resolves after 10 ms, so 'b' logs next.
- Final order is a c b.
Why the other options are wrong
- A. b is printed after a 10 ms wait, so it comes after c, not before it.
- B. c is logged after main() has already printed a and paused at the await, so c cannot come first.
- C. The line b is still printed after the 10 ms wait, so the output is not just a and c.
Tip: Remember: await pauses the function but lets other code execute immediately.
Question 7Medium
What does this print?
const wait = ms => new Promise(r => setTimeout(r, ms));
async function main() {
const out = [];
[1, 2, 3].forEach(async n => { await wait(10); out.push(n); });
console.log(out.length);
}
main();
- 3
- 1
- 0
- It waits, then prints 3
Answer: C. 0
Explanation
forEach ignores the promises returned by async callbacks, so nothing is awaited and the log runs before any push. Use for...of with await or Promise.all with map.
Step-by-step solution
- ForEach calls the async callback three times, but does not wait for the promises.
- Each callback waits 10 ms before it pushes, so no push has happened yet.
- console.log runs right away, and out is still empty, so it prints 0. The correct answer is C (0).
Why the other options are wrong
- A. The pushes happen after 10 ms, so the length is not 3 at that point.
- B. No callback has finished when the log runs, so the length is not 1.
- D. The log is synchronous and does not wait, so nothing is printed after a delay.
Tip: ForEach does not await async callbacks. Use for...of or Promise.all with map to wait for them.
Question 8Medium
What does this print?
const EventEmitter = require('events');
const e = new EventEmitter();
e.on('go', () => console.log('listener'));
console.log('before');
e.emit('go');
console.log('after');
- before after listener
- before listener after
- listener before after
- before after
Answer: B. before listener after
Explanation
EventEmitter.emit() calls the listeners synchronously, in registration order, before it returns.
Step-by-step solution
- console.log('before') prints 'before' first.
- e.emit('go') triggers the listener function which logs 'listener'.
- console.log('after') runs after the listener completes, logging 'after'.
- The order is before, listener, after as per synchronous execution.
- Final answer: before listener after.
Why the other options are wrong
- A. The listener runs during emit, which is before 'after' is printed, so it is not last.
- C. before is logged before emit runs, so 'listener' cannot come before it.
- D. emit calls the listener synchronously, so 'listener' is printed and not skipped.
Tip: Remember that event listeners are executed synchronously right after emit is called.
Question 9Hard
What does this print?
async function fail() { throw new Error('boom'); }
async function f() {
try { return fail(); } catch { return 'caught'; }
}
f().then(v => console.log('ok', v), e => console.log('err', e.message));
- ok caught
- err boom
- ok undefined
- err caught
Answer: B. err boom
Explanation
return fail() hands back the promise without awaiting it, so the rejection happens outside the try block. return await fail() would be caught.
Step-by-step solution
- fail() is async, so it returns a promise that rejects with 'boom'.
- F returns that promise without await, so the try block does not catch the rejection.
- F's promise follows the rejection, so the second callback runs with 'boom'. The correct answer is B (err boom).
Why the other options are wrong
- A. The catch never runs, so 'caught' is not returned and the first callback does not run.
- C. The promise is rejected, not resolved with undefined.
- D. The rejection is passed on as the error 'boom', not as 'caught'.
Tip: Use return await inside try, otherwise a rejected promise skips the catch block.
Question 10Hard
Roughly what does this print (times rounded to tenths of a second)?
const wait = ms => new Promise(r => setTimeout(r, ms));
async function main() {
let t = Date.now();
await wait(100); await wait(100);
const a = Date.now() - t;
t = Date.now();
await Promise.all([wait(100), wait(100)]);
const b = Date.now() - t;
console.log(Math.round(a / 100), Math.round(b / 100));
}
main();
- 2 1
- 1 1
- 2 2
- 1 2
Answer: A. 2 1
Explanation
Two awaits in a row run one after the other (~200 ms). Promise.all starts both timers together, so they finish in ~100 ms.
Step-by-step solution
- The first timing uses two awaits in a row, each 100 ms, so about 200 ms in total. 200 / 100 rounds to 2.
- The second timing uses Promise.all, so both 100 ms timers run together. About 100 ms in total, which rounds to 1.
- So the output is '2 1'. The correct answer is A (2 1).
Why the other options are wrong
- B. The sequential awaits take about 200 ms, so the first number is 2, not 1.
- C. Promise.all runs the timers in parallel, so the second number is 1, not 2.
- D. The order of the two numbers is reversed; the sequential one is 2.
Tip: Sequential await adds the waits together; Promise.all runs them side by side and takes about the longest one.
Practise all 17 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 →