Implement Custom Promise

A Promise is a fundamental concept in JavaScript for handling asynchronous operations. Understanding how to implement a custom Promise will deepen your understanding of asynchronous programming and help you in interviews.

Problem Statement

Implement a custom Promise class with the following methods:

  • constructor(executor) - Initialize the promise with an executor function
  • then(onFulfilled, onRejected) - Handle successful and failed states
  • catch(onRejected) - Handle only rejected states
  • finally(onFinally) - Execute regardless of state
  • static resolve(value) - Create a resolved promise
  • static reject(reason) - Create a rejected promise
  • static all(promises) - Wait for all promises to resolve
  • static race(promises) - Return the first settled promise
Implement Custom Promise | Zen Frontend