JavaScript Promises and Async/Await Interview Questions
Last updated 2026-11-19
Asynchronous JavaScript is a staple of mid-level interviews. You are expected to explain promise states, chain correctly, handle errors in both promise and async/await styles, pick the right combinator, and predict the order in which logs appear when promises, timers and synchronous code mix.
The answers below include the standard polyfill exercises, such as writing Promise.all and a retry helper, plus the mistakes that cause real bugs: await inside forEach, sequential awaits that should be parallel and rejections nobody handles.
| Level | 1 to 5 years |
|---|---|
| Focus | Promises, async/await, combinators, ordering |
| Runtime | Browser and Node.js 18 or later |
Ordering rules to memorise
Synchronous code runs first, then all queued microtasks, which include promise reactions and queueMicrotask callbacks, and only then the next timer or other task. An await splits a function: the code after it runs later as a microtask.
- Promise callbacks are always asynchronous, even for an already resolved promise
- An async function always returns a promise
- A thrown error inside then or an async function becomes a rejection
See also
The scheduling details are explained in the event loop, and the closure ideas behind callbacks in JavaScript closures. For a study plan, read how to crack a JavaScript interview.
JavaScript Promises and Async/Await Interview Questions: questions with worked answers
1 What is a promise, and what are its states?
easyA promise represents the eventual result of an asynchronous operation. It starts pending and settles exactly once, either fulfilled with a value or rejected with a reason. After settling it never changes. Handlers added with then, catch and finally run asynchronously, even if the promise is already settled.
2 Predict the output: sync code, promise and timer order.
hardSynchronous statements run first, so 1 and 4 print. The promise callback is a microtask, which runs as soon as the stack is empty, so 3 comes before the timer callback, which is a macrotask, so 2 is last even with a zero delay.
javascriptconsole.log(1); setTimeout(() => console.log(2), 0); Promise.resolve().then(() => console.log(3)); console.log(4); // 1 4 3 23 Predict the output: how does await order things?
hardCalling an async function runs it synchronously until its first await. After await, the rest of that function is scheduled as a microtask. So a1 and b print during the call to a, then the caller continues with e, and a2 runs afterwards.
javascriptasync function b() { console.log("b"); } async function a() { console.log("a1"); await b(); console.log("a2"); } console.log("s"); a(); console.log("e"); // s a1 b e a24 How does promise chaining work, and what does then return?
mediumthen always returns a new promise. If the handler returns a plain value, the next promise fulfils with it. If it returns a promise, the chain waits for it. If it throws, the next promise rejects. Forgetting to return inside then breaks the chain, so the next step receives undefined.
javascriptPromise.resolve(1) .then((x) => x + 1) .then((x) => { x * 10; }) // no return .then((x) => console.log(x)); // undefined5 How do you handle errors with promises and with async/await?
mediumWith promises, add catch at the end of the chain, which handles rejections from any earlier step and from thrown errors. With async functions, wrap awaits in try and catch. A catch that does not rethrow turns the chain back into a fulfilled one. Always handle or propagate rejections, because an unhandled rejection can crash a Node process.
javascriptconst fail = () => Promise.reject(new Error("boom")); async function run() { try { await fail(); } catch (e) { console.log("caught", e.message); // caught boom } finally { console.log("done"); // done } } run();6 Compare Promise.all, allSettled, race and any.
mediumPromise.all fulfils with an array of results when all fulfil and rejects as soon as one rejects. allSettled waits for all and reports each outcome as status and value or reason, never rejecting. race settles with the first promise to settle, whether fulfilled or rejected. any fulfils with the first fulfilment and rejects with an AggregateError only if all reject. Use allSettled for independent best-effort tasks and race for timeouts.
javascriptconst ok = (v, ms) => new Promise((r) => setTimeout(() => r(v), ms)); const no = (ms) => new Promise((_, j) => setTimeout(() => j(new Error("x")), ms)); Promise.all([ok(1, 10), ok(2, 5)]).then(console.log); // [1, 2] Promise.race([ok("slow", 50), ok("fast", 5)]).then(console.log); // fast Promise.any([no(5), ok("yes", 20)]).then(console.log); // yes Promise.allSettled([ok(1, 1), no(1)]).then((r) => console.log(r.map((x) => x.status))); // [ 'fulfilled', 'rejected' ]7 Why is sequential await slower than parallel, and how do you fix it?
mediumAwaiting inside a loop or one after another waits for each task before starting the next, so total time is the sum. If the tasks are independent, start them all first and await them together with Promise.all, so the total is roughly the slowest task.
javascriptconst wait = (ms) => new Promise((r) => setTimeout(r, ms)); async function seq() { await wait(100); await wait(100); } // ~200ms async function para() { await Promise.all([wait(100), wait(100)]); } // ~100ms8 Why does await inside forEach not behave as expected?
hardforEach ignores the return value of its callback, so the promises returned by an async callback are not awaited, and the code after the loop runs immediately. Use for...of with await for sequential work, or Promise.all with map for parallel work.
javascriptconst wait = (ms) => new Promise((r) => setTimeout(r, ms)); async function main() { [1, 2, 3].forEach(async (n) => { await wait(10); console.log("item", n); }); console.log("after forEach"); // prints first for (const n of [4, 5]) { await wait(10); console.log("item", n); } await Promise.all([6, 7].map(async (n) => { await wait(10); console.log("item", n); })); } main();9 Implement Promise.all from scratch.
hardReturn a new promise. For each input, wrap it with Promise.resolve, store its result at its index so order is preserved, count completions and resolve when the count reaches the length. Reject immediately on the first rejection. Handle the empty input case by resolving at once.
javascriptfunction promiseAll(items) { return new Promise((resolve, reject) => { const out = []; let left = items.length; if (left === 0) return resolve(out); items.forEach((p, i) => { Promise.resolve(p).then((v) => { out[i] = v; if (--left === 0) resolve(out); }, reject); }); }); } promiseAll([1, Promise.resolve(2)]).then(console.log); // [1, 2]10 How do you convert a callback API into a promise?
mediumWrap the call in new Promise, passing resolve and reject into the callback. For Node style error-first callbacks, reject on the error argument and resolve otherwise. Node also provides util.promisify, and many core modules have promise versions.
javascriptconst delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); function readP(fs, path) { return new Promise((res, rej) => fs.readFile(path, "utf8", (err, data) => (err ? rej(err) : res(data)))); } delay(10).then(() => console.log("tick")); // tick11 Write a retry helper with exponential backoff.
hardLoop up to a maximum number of attempts, try the operation, return on success, and on failure wait for a growing delay before the next attempt. After the last failure, rethrow the final error. Add jitter in production to avoid retry storms, and only retry errors that are likely transient.
javascriptconst wait = (ms) => new Promise((r) => setTimeout(r, ms)); async function retry(fn, tries = 3, base = 100) { for (let i = 0; ; i++) { try { return await fn(); } catch (e) { if (i === tries - 1) throw e; await wait(base * 2 ** i); } } }12 What does finally do in a promise chain?
mediumfinally runs a callback whether the promise fulfilled or rejected, receives no argument and passes the original result or error through unchanged, unless the callback throws or returns a rejected promise. It is intended for cleanup such as hiding a spinner.
javascriptPromise.resolve("v") .finally(() => console.log("cleanup")) .then((x) => console.log(x)); // cleanup, then v13 Predict the output: what does an async function return, and what does return await change?
hardAn async function always returns a promise, wrapping any returned value. Inside try and catch, return await keeps the rejection inside the try so the catch can handle it, whereas a plain return of the promise lets the rejection escape the function.
javascriptasync function f() { return 1; } console.log(f() instanceof Promise); // true async function g() { try { return await Promise.reject(new Error("e")); } catch { return "handled"; } } g().then(console.log); // handled14 How do you add a timeout to a promise?
mediumRace the operation against a timer promise that rejects after the delay. The loser still keeps running because promises cannot be cancelled, so for fetch pass an AbortSignal, for example AbortSignal.timeout(ms).
javascriptconst timeout = (p, ms) => Promise.race([p, new Promise((_, j) => setTimeout(() => j(new Error("timeout")), ms))]); timeout(new Promise(() => {}), 20).catch((e) => console.log(e.message)); // timeout
Frequently asked questions
What is the difference between promises and async/await?
Async/await is syntax over promises. An await pauses the function until the promise settles, and the surrounding code reads like synchronous code, with try and catch for errors.
What happens to an unhandled promise rejection?
Browsers fire an unhandledrejection event and log an error. In Node 15 and later an unhandled rejection terminates the process by default.
Can I cancel a promise?
Not directly. Use AbortController for APIs that support it, or ignore stale results with a flag.
Is top-level await allowed?
Yes in ES modules, in modern browsers and Node. It is not allowed in classic scripts or CommonJS files.
Related guides
More in Programming Language Interview Questions by Language
- JavaScript Prototypes and Inheritance Interview Questions
- JavaScript this Keyword Interview Questions
- JVM Internals Interview Questions
- MySQL Interview Questions and Answers
- Nth Highest Salary in SQL: Interview Questions
- PL/SQL Interview Questions and Answers
- PostgreSQL Interview Questions and Answers
- Python Coding Interview Questions with Solutions
- Python Decorators Interview Questions
- Python Exception Handling Interview Questions
- Python File Handling Interview Questions
- Python Generators and Iterators Interview Questions