Almost every real JavaScript program waits for something — a network call, a timer, a user click — so asynchronous code is the most-tested area in front-end interviews. This guide covers the event loop, callbacks, promises, async/await, error handling and the four promise combinators.
Why interviewers ask about JavaScript promises and async/await
Async bugs are the hardest to spot in code review: requests that run one after another when they could run in parallel, errors that vanish, state that updates in the wrong order. Interviewers ask these questions because they predict whether you will write those bugs.
The event-loop output question in particular is a favourite. It is short, it has one right answer, and explaining it well shows you understand how JavaScript actually schedules work.
How to answer these questions
For output questions, label each line as synchronous, microtask or macrotask before you say the answer. For design questions, state whether the operations depend on each other — dependent calls are awaited in sequence, independent ones go into Promise.all.
Closures appear inside almost every callback here, so if they still feel shaky, revise the closures and functions guide first.
Q1. What is the event loop in JavaScript?
The event loop is JavaScript's concurrency model. Because JS is single-threaded, the loop runs synchronous code first, then all microtasks (promises, queueMicrotask), then one macrotask (setTimeout, setInterval, I/O), and repeats.
console.log("1");
setTimeout(() => console.log("3"), 0);
Promise.resolve().then(() => console.log("2"));
// Output: 1 2 3
Interview note: This exact snippet is the most common output question. Explain it in order:
1is synchronous, the promise callback is a microtask and runs before the timer, which is a macrotask — even with 0 ms.
Q2. What are promises in JavaScript?
A promise represents a value that may be available now, later or never. It has three states — pending, fulfilled and rejected — and you react to it with .then, .catch and .finally.
const p = new Promise((resolve, reject) => {
setTimeout(() => resolve("done"), 1000);
});
p
.then(val => console.log(val)) // "done"
.catch(err => console.error(err))
.finally(() => console.log("cleanup"));
Q3. What is a callback, and what is callback hell?
A callback is a function passed to another function and called when an operation finishes. Callback hell is deeply nested callbacks that become hard to read and handle errors in — promises and async/await flatten it.
// Callback hell (pyramid of doom)
getUser(id, (user) => {
getPosts(user.id, (posts) => {
getComments(posts[0].id, (comments) => {
// deeply nested...
});
});
});
// Solution: async/await
const user = await getUser(id);
const posts = await getPosts(user.id);
const comments = await getComments(posts[0].id);
Q4. What is async/await and how does it relate to promises?
async/await is syntax over promises. An async function always returns a promise, and await pauses that function until the awaited promise settles — so asynchronous code reads like synchronous code.
async function fetchUser(id) {
try {
const res = await fetch(`/api/users/${id}`);
if (!res.ok) throw new Error("Not found");
return await res.json();
} catch (err) {
console.error(err);
}
}
Interview note: Trap: two
awaits in a row run one after the other. If the calls are independent, start both andawait Promise.all([...])so they run in parallel.
Q5. How do you handle errors in promises and async/await?
With promises, add a .catch() at the end of the chain; with async/await, wrap the awaited calls in try/catch. An error thrown at any step skips to the nearest handler, and a rejection that nobody handles triggers an "unhandled rejection" warning.
// Promise chain
fetchUser()
.then(user => fetchOrders(user.id))
.then(showOrders)
.catch(err => console.error("Failed:", err)); // catches any step
// async/await
async function load() {
try {
const user = await fetchUser();
const orders = await fetchOrders(user.id);
showOrders(orders);
} catch (err) {
console.error("Failed:", err);
} finally {
hideSpinner();
}
}
Interview note: Trap:
fetchdoes not reject on an HTTP 404 or 500 — it only rejects when the network fails. Checkres.okand throw the error yourself.
Q6. What is Promise.all() and when would you use it?
Promise.all() takes an array of promises and resolves when all of them resolve — or rejects as soon as any one rejects.
const [user, posts] = await Promise.all([
fetch("/api/user").then(r => r.json()),
fetch("/api/posts").then(r => r.json()),
]);
// Both requests run in parallel — not one after the other
Use it when several asynchronous operations are independent and you need all of their results.
Q7. What is the difference between Promise.race() and Promise.allSettled()?
Promise.race() settles with the first promise that settles, win or lose. Promise.allSettled() waits for all of them and returns an array of { status, value/reason } objects — it never rejects.
// race — timeout pattern
const result = await Promise.race([
fetch("/api/slow"),
new Promise((_, rej) => setTimeout(() => rej("timeout"), 3000)),
]);
// allSettled — you want every outcome, even if some fail
const results = await Promise.allSettled([p1, p2, p3]);
results.forEach(r => {
if (r.status === "fulfilled") console.log(r.value);
else console.warn(r.reason);
});
Q8. What does Promise.any() do?
Promise.any() resolves with the first promise that fulfils and ignores rejections along the way. It rejects only if every promise rejects, with an AggregateError that holds all the reasons.
const response = await Promise.any([
fetch("https://mirror-1.example.com/data"),
fetch("https://mirror-2.example.com/data"),
]);
// uses whichever mirror answers successfully first
Know the four combinators side by side: all (every promise must succeed), allSettled (wait for every result), race (first to settle, success or failure) and any (first success).
How to prepare
Write the classic ordering puzzle — console.log, setTimeout(…, 0), Promise.resolve().then(…) — and extend it with nested promises and await until you can predict every variant. Then build a small page that fetches two APIs in parallel, shows a spinner, and handles a failed request properly.
After this, the browser APIs and performance guide covers debouncing, Web Workers and lazy loading, which build on the same ideas.
Frequently Asked Questions
What is the difference between a microtask and a macrotask?
Is async/await better than promises?
Does await block the whole program?
How do I run several API calls in parallel?
What happens if I forget to handle a rejected promise?
Is JavaScript truly asynchronous if it is single-threaded?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Explore the Java Full Stack program

