JavaScript Promises Interview Questions & Answers
Promises are a core feature of modern JavaScript for handling asynchronous operations. They provide a cleaner, more flexible alternative to callbacks and are foundational for async/await.
Table of Contents
- What is a Promise?
- Promise States
- Creating Promises
- Consuming Promises
- Promise Chaining
- Error Handling
- Promise Utilities
- Common Pitfalls
- Interview Questions
What is a Promise?
A Promise is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value.
- Promises help avoid “callback hell” and make async code easier to read and maintain.
Promise States
A Promise can be in one of three states:
| State | Description |
|---|---|
| pending | Initial state, neither fulfilled nor rejected |
| fulfilled | Operation completed successfully |
| rejected | Operation failed |
Creating Promises
const promise = new Promise((resolve, reject) => {
// async operation
if (/* success */) {
resolve('Result');
} else {
reject('Error');
}
});
Example:
function asyncTask(success) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (success) {
resolve('Task completed!');
} else {
reject('Task failed!');
}
}, 1000);
});
}
Consuming Promises
- Use
.then()for success,.catch()for errors, and.finally()for cleanup.
asyncTask(true)
.then(result => console.log(result))
.catch(error => console.error(error))
.finally(() => console.log('Done!'));
Promise Chaining
- Promises can be chained for sequential async operations.
asyncTask(true)
.then(result => {
console.log(result);
return asyncTask(false);
})
.then(result => console.log(result))
.catch(error => console.error('Caught:', error));
Error Handling
- Errors thrown in
.then()are caught by the next.catch(). - Always end promise chains with
.catch()to handle errors.
asyncTask(false)
.then(result => {
throw new Error('Something went wrong!');
})
.catch(error => console.error(error));
Promise Utilities
Promise.all([p1, p2, ...])– Waits for all promises to resolve or any to reject.Promise.race([p1, p2, ...])– Resolves/rejects as soon as one promise settles.Promise.allSettled([p1, p2, ...])– Waits for all to settle (fulfilled or rejected).Promise.any([p1, p2, ...])– Resolves as soon as one fulfills, rejects if all reject.
Example:
const p1 = Promise.resolve(1);
const p2 = Promise.resolve(2);
const p3 = Promise.reject('fail');
Promise.all([p1, p2])
.then(values => console.log(values)); // [1, 2]
Promise.race([p1, p2])
.then(value => console.log(value)); // 1
Promise.allSettled([p1, p3])
.then(results => console.log(results));
// [{status: 'fulfilled', value: 1}, {status: 'rejected', reason: 'fail'}]
Promise.any([p3, p1])
.then(value => console.log(value)); // 1
Common Pitfalls
- Not returning promises in chains
- Forgetting to handle errors
- Mixing callbacks and promises
- Creating unhandled promise rejections
Interview Questions
- What is a Promise and why is it useful?
- A Promise is an object representing the eventual result of an async operation. It helps manage async code and avoid callback hell.
- What are the three states of a Promise?
- pending, fulfilled, rejected
- How do you create a Promise?
- Using
new Promise((resolve, reject) => { ... })
- Using
- How do you consume a Promise?
- Using
.then(),.catch(), and.finally()
- Using
- What is promise chaining?
- Linking multiple
.then()calls for sequential async operations.
- Linking multiple
- How do you handle errors in promises?
- Using
.catch()or the second argument of.then()
- Using
- What does
Promise.alldo?- Waits for all promises to resolve or any to reject.
- What is the difference between
Promise.allandPromise.race?allwaits for all,raceresolves/rejects as soon as one settles.
- What is an unhandled promise rejection?
- When a promise is rejected and no
.catch()handles the error.
- When a promise is rejected and no
- How do you convert a callback-based function to a promise-based one?
- Use the Promise constructor or
util.promisifyin Node.js.
Additional Resources
Mastering Promises is essential for modern JavaScript development and interview success.
Interview angle
- “What are the promise states?” - pending, fulfilled, rejected. Settlement is one-way and permanent, which is the guarantee callbacks do not give.
- “
allversusallSettledversusraceversusany?” -allrejects on the first failure and is right when you need every result;allSettledalways resolves with per-item status and is right for partial success;racesettles on the first to settle either way;anyresolves on the first success. Choosingallwhen you wanted partial results is the common mistake. - “Does
Promise.allcancel the others on rejection?” - no. The remaining promises keep running and their results are discarded, and a later rejection among them can become unhandled. Cancellation needsAbortController. - “Microtask or macrotask?” - promise callbacks run as microtasks, draining fully before the next macrotask such as
setTimeout. That is why a.thenscheduled after asetTimeout(0)still runs first. - “What is
Promise.withResolvers?” - ES2024 sugar returning the promise plus itsresolve/reject, replacing the deferred pattern of hoisting them out of the executor.