In JavaScript, a Promise is an object that represents the eventual completion or failure of an asynchronous operation, and its resulting value. It allows you to handle asynchronous operations more effectively by chaining multiple asynchronous operations, and handling their results or errors using the then() method.
Example:
const myPromise = new Promise((resolve, reject) => {
// Asynchronous operation
if (/* operation successful */) {
resolve('Operation result');
} else {
reject('Error message');
}
});
myPromise.then(result => {
console.log(result);
}).catch(error => {
console.error(error);
});