
Async JavaScript gives interviewers more tricky questions than any other topic. This set is about the event loop, microtasks versus macrotasks, Promise helpers like all and race, and how async/await really runs. Work out the output order on paper before you tap an answer.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 16 questions.
Topics covered in this set
- Call stack, microtask queue and task (macrotask) queue
- setTimeout vs queueMicrotask vs Promise.then
- Promise chaining, catch and finally
- Promise.all, allSettled, race and any
- async/await execution order
- Sequential vs parallel awaits
JavaScript MCQs with answers and explanations
Q1. When do Promise .then() callbacks run relative to setTimeout callbacks?
a) After all setTimeout callbacks
b) Before setTimeout callbacks, because they are microtasks that run as soon as the call stack is empty
c) In random order
d) Only when the browser is idle
Q2. What is the output order?
console.log('A');
setTimeout(() => console.log('B'), 0);
queueMicrotask(() => console.log('C'));
console.log('D');
a) A D C B
b) A C D B
c) A B C D
d) A D B C
Q3. What is printed?
async function f() {
return 1;
}
console.log(f() instanceof Promise);
a) 1
b) TypeError
c) false
d) true
Q4. What is the output order?
async function f() {
console.log(1);
await null;
console.log(2);
}
f();
console.log(3);
a) 3 1 2
b) 1 2 3
c) 2 1 3
d) 1 3 2
Q5. How does Promise.all behave if one of the input promises rejects?
a) It rejects immediately with the first rejection reason
b) It waits for all promises and returns only the fulfilled ones
c) It retries the rejected promise
d) It resolves with undefined for the rejected one
Q6. What does Promise.allSettled resolve with?
a) The first settled value
b) An array of objects with a status of 'fulfilled' or 'rejected' plus a value or reason
c) Only the values of fulfilled promises
d) It never resolves if one promise rejects
Q7. Which statement about Promise.any is correct?
a) It is the same as Promise.all
b) It resolves with the first promise to settle, fulfilled or rejected
c) It rejects as soon as any promise rejects
d) It resolves with the first fulfilled value and rejects with an AggregateError only if all inputs reject
Q8. What is the output order?
new Promise((resolve) => {
console.log('X');
resolve();
});
console.log('Y');
a) Only Y
b) Only X
c) Y X
d) X Y
Q9. What is printed?
Promise.resolve(1)
.then((x) => x + 1)
.then((x) => { throw x * 2; })
.catch((e) => e + 1)
.then(console.log);
a) 4
b) Nothing, it is an unhandled rejection
c) 2
d) 5
Q10. What happens here?
try {
Promise.reject(new Error('x'));
} catch (e) {
console.log('caught');
}
a) Logs caught
b) Nothing is logged by catch and an unhandled rejection is reported
c) Throws a SyntaxError
d) Logs x
Q11. wait(ms) returns a promise that resolves after ms. Roughly how long does each version take?
// Version 1
await wait(1000);
await wait(1000);
// Version 2
await Promise.all([wait(1000), wait(1000)]);
a) Both about 2 seconds
b) Both about 1 second
c) Version 1 about 1 second, version 2 about 2 seconds
d) Version 1 about 2 seconds, version 2 about 1 second
Q12. What is the problem with this code?
items.forEach(async (item) => {
await save(item);
});
console.log('done');
a) It throws a SyntaxError
b) 'done' is logged before the saves finish because forEach does not wait for async callbacks
c) There is no problem
d) save is called only once
Q13. What does setTimeout(fn, 0) guarantee?
a) fn runs before any Promise callback
b) fn runs immediately
c) fn runs exactly 0 ms later
d) fn runs after the current synchronous code and pending microtasks, after a minimum delay
Q14. What is printed?
Promise.resolve(2)
.finally(() => 10)
.then(console.log);
a) 12
b) 2
c) undefined
d) 10
Q15. What is the output order?
setTimeout(() => console.log('t1'), 0);
Promise.resolve()
.then(() => {
console.log('p1');
setTimeout(() => console.log('t2'), 0);
})
.then(() => console.log('p2'));
a) p1 p2 t2 t1
b) p1 t1 p2 t2
c) t1 p1 p2 t2
d) p1 p2 t1 t2
Q16. Which statement about JavaScript concurrency in the browser is correct?
a) JavaScript runs every callback on a separate thread
b) Promises create new threads
c) Your code runs on a single main thread; timers and network I/O are handled by the browser and their callbacks are queued
d) async/await makes code run in parallel on multiple cores
Quick revision notes
- Order: synchronous code first, then all microtasks, then one task (macrotask), and then the cycle repeats.
- Microtasks: Promise reactions, queueMicrotask, await continuations. Tasks: setTimeout, setInterval, I/O, UI events.
- The Promise executor runs synchronously; .then callbacks never do.
- all = fail-fast, allSettled = every result, race = first settled, any = first fulfilled.
- Run independent async work in parallel with Promise.all. Do not use async callbacks inside forEach.
Frequently asked questions
What is the difference between microtasks and macrotasks?
Microtasks (promise callbacks, queueMicrotask) run right after the current script, and the whole queue is emptied every time. Macrotasks (timers, I/O, events) run one per loop turn, only after the microtask queue is empty.
Is async/await faster than Promises?
No. async/await is just nicer syntax on top of Promises. It makes code easier to read, but speed depends on whether you run independent work in parallel.
Why does setTimeout with 0 ms not run immediately?
Because it only queues a task. The callback must wait until the current code and every queued microtask have finished.
Comments
Post a Comment