Top 10 JavaScript Promise Questions Asked in Interviews

The Syntax NodeThe Syntax Node
4 min read

Promises in JavaScript are a crucial concept for handling asynchronous operations. Understanding them is key for developers, especially during interviews. Here are the top 10 frequently asked JavaScript Promise questions, along with their answers and explanations.


1. What is a Promise in JavaScript?

A Promise is an object in JavaScript used for handling asynchronous operations. It represents a value that may be available now, or in the future, or never. Promises have three states:

  • Pending: Initial state, neither fulfilled nor rejected.

  • Fulfilled: Operation completed successfully.

  • Rejected: Operation failed.

Example:

const promise = new Promise((resolve, reject) => {
  if (Math.random() > 0.5) {
    resolve('Success!');
  } else {
    reject('Failure.');
  }
});

2. How is a Promise different from a callback?

Promises provide a cleaner and more maintainable way to handle asynchronous operations compared to callbacks. Key differences include:

  • Promises avoid callback hell by chaining .then() methods.

  • They improve error handling using .catch().

  • Promises are more readable and easier to debug.


3. What are the methods available on Promises?

Some commonly used methods are:

  • Promise.resolve(value): Returns a resolved Promise.

  • Promise.reject(reason): Returns a rejected Promise.

  • Promise.all(promises): Resolves when all Promises resolve, or rejects if any Promise rejects.

  • Promise.race(promises): Resolves or rejects as soon as any Promise settles.

  • Promise.any(promises): Resolves as soon as any Promise fulfills or rejects if all Promises reject.

  • Promise.allSettled(promises): Resolves after all Promises settle (fulfilled or rejected).


4. What is the difference between Promise.all and Promise.allSettled?

  • Promise.all: Resolves only if all Promises resolve; rejects if any Promise rejects.

  • Promise.allSettled: Resolves after all Promises settle, regardless of whether they fulfill or reject.

Example:

const promises = [
  Promise.resolve('A'),
  Promise.reject('B'),
  Promise.resolve('C')
];

Promise.all(promises)
  .then(console.log) // Won't execute
  .catch(console.error); // Logs: 'B'

Promise.allSettled(promises)
  .then(console.log); // Logs the status of all Promises

5. How can you chain Promises?

You can chain Promises by returning another Promise from the .then() block. This allows you to perform sequential asynchronous operations.

Example:

fetch('/api/data')
  .then(response => response.json())
  .then(data => {
    console.log(data);
    return fetch('/api/more-data');
  })
  .then(response => response.json())
  .then(moreData => console.log(moreData))
  .catch(error => console.error(error));

6. What is async/await and how does it relate to Promises?

async/await is syntactic sugar for working with Promises, making asynchronous code look synchronous.

  • An async function always returns a Promise.

  • await pauses the execution of an async function until the Promise resolves.

Example:

async function fetchData() {
  try {
    const response = await fetch('/api/data');
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error(error);
  }
}

7. How do you handle errors in Promises?

Errors can be handled using:

  • .catch() to catch errors in the Promise chain.

  • A try...catch block when using async/await.

Example:

// Using .catch()
promise
  .then(result => console.log(result))
  .catch(error => console.error(error));

// Using async/await
async function fetchData() {
  try {
    const data = await somePromise;
    console.log(data);
  } catch (error) {
    console.error(error);
  }
}

8. What is the difference between microtasks and macrotasks?

  • Microtasks: Include Promises and MutationObserver callbacks. They have higher priority and are executed before macrotasks.

  • Macrotasks: Include setTimeout, setInterval, and setImmediate. These are executed after the current event loop completes.

Example:

console.log('Start');

setTimeout(() => console.log('Macrotask'), 0);

Promise.resolve().then(() => console.log('Microtask'));

console.log('End');

// Output: Start -> End -> Microtask -> Macrotask

9. What happens if you don’t handle a rejected Promise?

If a rejected Promise isn’t handled, it results in an UnhandledPromiseRejectionWarning in Node.js or similar warnings in browsers. It’s important to always handle rejections to avoid unexpected behavior.

Example:

const promise = new Promise((resolve, reject) => {
  reject('Error occurred');
});

// Without .catch(), this will cause an UnhandledPromiseRejectionWarning
promise.catch(error => console.error(error));

10. Can you cancel a Promise?

JavaScript doesn’t natively support canceling Promises. However, you can implement cancellation using flags or libraries like AbortController for specific use cases like fetch.

Example:

const controller = new AbortController();
fetch('/api/data', { signal: controller.signal })
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => {
    if (error.name === 'AbortError') {
      console.log('Fetch aborted');
    }
  });

// Cancel the fetch request
controller.abort();

These questions will not only help you ace JavaScript interviews but also deepen your understanding of Promises. Practice these concepts, and you’ll be ready to handle any asynchronous challenge! Happy Learning :)

0
Subscribe to my newsletter

Read articles from The Syntax Node directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

The Syntax Node
The Syntax Node

I am a JavaScript Full Stack Developer with expertise in front-end frameworks like React and Angular, and back-end technologies such as Node.js and Express.