Promise Chaining Interview Questions in JavaScript
Promise chaining is a top interview topic. Here are the most asked questions with code.
Promise Chaining Interview Questions in JavaScript
Promise chaining is a top interview topic. Here are the most asked questions with code and answers.
Q1: What is the output?
Promise.resolve(1) .then((n) => n + 1) .then((n) => console.log(n)); `` **Answer**: `2`. Each `.then` passes its return value to the next. ### Q2: What is the output? ```js Promise.resolve(1) .then((n) => { n + 1; }) .then((n) => console.log(n)); `` **Answer**: `undefined`. The first `.then` has a block body with no `return`. ### Q3: What is the output? ```js Promise.resolve(1) .then((n) => Promise.resolve(n + 1)) .then((n) => console.log(n)); `` **Answer**: `2`. If a `.then` returns a promise, the next `.then` waits for it. ### Q4: What is the output? ```js Promise.reject("err") .then((v) => console.log("then", v)) .catch((e) => console.log("catch", e)); `` **Answer**: `catch err`. The `.then` is skipped (rejected); `.catch` runs. ### Q5: What is the output? ```js Promise.resolve(1) .then((n) => { throw new Error("oops"); }) .then((n) => console.log("skipped")) .catch((err) => console.log(err.message)); `` **Answer**: `oops`. The throw skips the next `.then` and goes to `.catch`. ### Q6: What is the output? ```js Promise.resolve(1) .catch((err) => console.log("catch")) .then((n) => console.log("then", n)); `` **Answer**: `then 1`. `.catch` only runs on rejection. Fulfilled, so `.catch` is skipped, `.then` receives 1. ### Q7: What is the output? ```js Promise.resolve(1) .then((n) => { throw "err"; }) .catch((e) => "recovered") .then((n) => console.log(n)); `` **Answer**: `recovered`. `.catch` returns "recovered", so the next `.then` receives it. ### Q8: How do you run promises in parallel? ```js Promise.all([fetchA(), fetchB(), fetchC()]) .then(([a, b, c]) => console.log(a, b, c)); `` ### Q9: How do you sequence promises? Chain `.then` calls or use `async/await` with sequential `await`. ### Q10: What is the difference between .then's second arg and .catch? `.then(onFulfilled, onRejected)` second arg only catches rejections from the original promise. `.catch` catches rejections and throws from any `.then` above it. ### The Takeaway Promise chaining interview questions test: returning values, not returning (undefined), returning promises (wait), error handling (`.catch` catches throws), `.catch` on fulfilled (skipped), recovery after `.catch`, parallel (`Promise.all`), and `.then` second arg vs `.catch`.
undefined. The first .then has a block body (braces) with no return statement. Block bodies need an explicit return. Without it, the function returns undefined, and the next .then receives undefined.
No. .catch only runs on rejection or when a .then above it throws. If the promise is fulfilled, .catch is skipped and the next .then receives the value.
The chain continues as fulfilled with the value returned by .catch. If .catch returns 'recovered', the next .then receives 'recovered'. If .catch re-throws, the next .catch catches it.
.then(onFulfilled, onRejected) second arg only catches rejections from the original promise. .catch catches rejections from the original promise AND thrown errors from any .then above it. .catch is more comprehensive.
then 1. .catch only runs on rejection. Since the promise is fulfilled (resolve(1)), .catch is skipped, and .then receives 1.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

