Dhananjay Patel Logo
  1. Home
  2. / Blog
  3. / Promis Async Await
  4. / Lessons
  5. / 4

Promises in JavaScript

  • A Promise is an object representing the eventual completion or failure of an asynchronous operation.

States of a Promise:

  1. Pending: The initial state, neither fulfilled nor rejected.
  2. Fulfilled: The operation was completed successfully.
  3. Rejected: The operation failed.
let promise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve('Operation successful');
} else {
reject('Operation failed');
}
});
promise.then((message) => {
console.log(message);
}).catch((error) => {
console.error(error);
});