Promise in JavaScript

Promise

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

A Promise has three states:

Pending – the operation is still going on.
Fulfill…


This content originally appeared on DEV Community and was authored by Bhuvana Sri R

Promise

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

A Promise has three states:

  • Pending – the operation is still going on.
  • Fulfilled – the operation completed successfully (resolve).
  • Rejected – the operation failed (reject).

Example :

let promise = new Promise((resolve, reject) => {
  // Do some task (like fetching data)

  let success = true; 

  if (success) {
    resolve("Task completed successfully!");
  } else {
    reject("Task failed!");
  }
});

// Using .then() and .catch()
promise
  .then(result => {
    console.log("Success:", result);
  })
  .catch(error => {
    console.log("Error:", error);
  });

Example :

function getData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      let dataFetched = true;

      if (dataFetched) {
        resolve("Data received!");
      } else {
        reject("Failed to fetch data.");
      }
    }, 2000);
  });
}

getData()
  .then(response => console.log(response))  // Success
  .catch(error => console.log(error));      // Failure

Promise Methods :

  1. - .then() → runs when promise is resolved.
  2. - .catch() → runs when promise is rejected.
  3. - .finally() → runs always (success or failure).
  4. - Promise.all([p1, p2, ...]) → waits for all promises.
  5. - Promise.race([p1, p2, ...]) → returns the first settled promise.


This content originally appeared on DEV Community and was authored by Bhuvana Sri R


Print Share Comment Cite Upload Translate Updates
APA

Bhuvana Sri R | Sciencx (2025-09-10T06:14:54+00:00) Promise in JavaScript. Retrieved from https://www.scien.cx/2025/09/10/promise-in-javascript-3/

MLA
" » Promise in JavaScript." Bhuvana Sri R | Sciencx - Wednesday September 10, 2025, https://www.scien.cx/2025/09/10/promise-in-javascript-3/
HARVARD
Bhuvana Sri R | Sciencx Wednesday September 10, 2025 » Promise in JavaScript., viewed ,<https://www.scien.cx/2025/09/10/promise-in-javascript-3/>
VANCOUVER
Bhuvana Sri R | Sciencx - » Promise in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/10/promise-in-javascript-3/
CHICAGO
" » Promise in JavaScript." Bhuvana Sri R | Sciencx - Accessed . https://www.scien.cx/2025/09/10/promise-in-javascript-3/
IEEE
" » Promise in JavaScript." Bhuvana Sri R | Sciencx [Online]. Available: https://www.scien.cx/2025/09/10/promise-in-javascript-3/. [Accessed: ]
rf:citation
» Promise in JavaScript | Bhuvana Sri R | Sciencx | https://www.scien.cx/2025/09/10/promise-in-javascript-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.