DEV Community

M Ramavel
M Ramavel

Posted on

Promise JS

A Promise in JavaScript is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

It helps you handle asynchronous operations like data fetching, file reading, or timers — without deeply nested callbacks (callback hell).

Syntax

let promise = new Promise(function(resolve, reject) {
  if (/* success */) {
    resolve(result);  // Success
  } else {
    reject(error);    // Error
  }
});
Enter fullscreen mode Exit fullscreen mode

Promise States

  1. Pending – Initial state, neither fulfilled nor rejected.

  2. Fulfilled – Operation completed successfully.

  3. Rejected – Operation failed

Top comments (0)