JavaScript Promises Interview Questions & Answers

5 interview angles 4 min read source

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?

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

  1. 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.
  2. What are the three states of a Promise?
    • pending, fulfilled, rejected
  3. How do you create a Promise?
    • Using new Promise((resolve, reject) => { ... })
  4. How do you consume a Promise?
    • Using .then(), .catch(), and .finally()
  5. What is promise chaining?
    • Linking multiple .then() calls for sequential async operations.
  6. How do you handle errors in promises?
    • Using .catch() or the second argument of .then()
  7. What does Promise.all do?
    • Waits for all promises to resolve or any to reject.
  8. What is the difference between Promise.all and Promise.race?
    • all waits for all, race resolves/rejects as soon as one settles.
  9. What is an unhandled promise rejection?
    • When a promise is rejected and no .catch() handles the error.
  10. How do you convert a callback-based function to a promise-based one?
  • Use the Promise constructor or util.promisify in 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.
  • all versus allSettled versus race versus any?” - all rejects on the first failure and is right when you need every result; allSettled always resolves with per-item status and is right for partial success; race settles on the first to settle either way; any resolves on the first success. Choosing all when you wanted partial results is the common mistake.
  • “Does Promise.all cancel 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 needs AbortController.
  • “Microtask or macrotask?” - promise callbacks run as microtasks, draining fully before the next macrotask such as setTimeout. That is why a .then scheduled after a setTimeout(0) still runs first.
  • “What is Promise.withResolvers?” - ES2024 sugar returning the promise plus its resolve/reject, replacing the deferred pattern of hoisting them out of the executor.