Facebook Pixel

Promise APIs Interview Questions in JavaScript

Promise.all, allSettled, race, and any are top interview topics. Here are the most asked questions.

Promise APIs Interview Questions in JavaScript

Promise.all, allSettled, race, and any are top interview topics. Here are the most asked questions.

Q1: What is the difference between Promise.all and Promise.allSettled?

Promise.all rejects if any promise rejects (fast fail). Promise.allSettled waits for all and returns [{ status, value/reason }]. Never rejects.

Q2: What is the difference between Promise.race and Promise.any?

Promise.race returns the first to settle (fulfilled or rejected). Promise.any returns the first to fulfill (ignores rejections). If all reject, any throws AggregateError.

Q3: What is the output?

Promise.all([Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]) .then((results) => console.log(results)); `` **Answer**: `[1, 2, 3]`. Results in order. ### Q4: What is the output? ```js Promise.all([Promise.resolve(1), Promise.reject("err"), Promise.resolve(3)]) .then((results) => console.log(results)) .catch((err) => console.log("catch", err)); `` **Answer**: `catch err`. `Promise.all` rejects if any rejects. ### Q5: What is the output? ```js Promise.allSettled([Promise.resolve(1), Promise.reject("err")]) .then((results) => console.log(results)); `` **Answer**: `[{ status: "fulfilled", value: 1 }, { status: "rejected", reason: "err" }]`. ### Q6: What is the output? ```js Promise.race([Promise.resolve("a"), Promise.resolve("b")]) .then((result) => console.log(result)); `` **Answer**: `"a"` (or `"b"`, whichever settles first). In practice, `"a"` because it was first in the array and both resolve synchronously. ### Q7: What is the output? ```js Promise.any([Promise.reject("err"), Promise.resolve("ok")]) .then((result) => console.log(result)); `` **Answer**: `"ok"`. `any` ignores rejections and returns the first fulfillment. ### Q8: What is the output? ```js Promise.any([Promise.reject("a"), Promise.reject("b")]) .catch((err) => console.log(err instanceof AggregateError, err.errors)); `` **Answer**: `true ["a", "b"]`. `any` rejects with `AggregateError` if all reject. ### Q9: What does Promise.race([]) do? Never settles (stays pending forever). ### Q10: What does Promise.any([]) do? Rejects with `AggregateError` (all reject, no promises to fulfill). ### The Takeaway Promise APIs interview questions test: `Promise.all` (fast fail, ordered results), `allSettled` (never rejects, status objects), `race` (first to settle), `any` (first to fulfill, `AggregateError` if all reject), and edge cases (empty arrays).

Promise.all rejects if any promise rejects (fast fail) and returns an array of values. Promise.allSettled waits for all promises, never rejects, and returns an array of { status, value/reason } objects.

Promise.race returns the first to settle (fulfilled or rejected). Promise.any returns the first to fulfill (ignores rejections). race can reject on the first rejection; any only rejects if all reject (with AggregateError).

An error type thrown by Promise.any when all promises reject. It has an errors property (an array of all individual rejection reasons). Check err instanceof AggregateError and access err.errors.

It never settles (stays pending forever). Be careful not to pass an empty array to Promise.race. Promise.any([]) is different: it rejects with AggregateError immediately.

Yes. The results array is in the same order as the input promises, regardless of which promise resolves first. This is one advantage over manual callback-based parallel execution.

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.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.