Category: React • Beginner
Published on 18 Feb 2026
Explanation
#white-A Promise in JavaScript is an object that #white-represents the eventual completion or #white-failure of an #white-asynchronous operation.
Code Example
let promise = new Promise((resolve, reject) => {
resolve("Success");
});
console.log(promise);
Explanation
#white-A Promise has three states: Pending (initial state), #white-Fulfilled (operation successful), and Rejected (operation failed).
Code Example
#white-// Pending -> Initial state #white-// Fulfilled -> resolve() called #white-// Rejected -> reject() called
Explanation
#white-The then() method is used to handle the #white-result when a Promise is fulfilled.
Code Example
let promise = new Promise((resolve) => {
resolve("Data received");
});
promise.then(result => console.log(result));
Explanation
#white-The catch() method is used to handle errors #white-when a Promise is rejected.
Code Example
let promise = new Promise((resolve, reject) => {
reject("Error occurred");
});
promise.catch(error => console.log(error));
Explanation
#white-Promises are commonly used for #white-#white-asynchronous tasks like #white-API calls or delayed operations.
Code Example
function fetchData() {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Data loaded");
}, 2000);
});
}
fetchData().then(data => console.log(data));
Explanation
#white-Promises can be chained using multiple then() methods #white-to perform sequential asynchronous operations.
Code Example
Promise.resolve(5) .then(num => num * 2) .then(result => console.log(result));