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
}
});
Promise States
Pending – Initial state, neither fulfilled nor rejected.
Fulfilled – Operation completed successfully.
Rejected – Operation failed
Top comments (0)