Promises are a way to handle asynchronous operations in JavaScript, representing a value that may be available now, or in the future, or never. They can be in one of three states: pending, fulfilled, or rejected. You can create a Promise like this:
const myPromise = new Promise((resolve, reject) => {
const success = true; // Simulate an operation
if (success) {
resolve('Operation was successful!');
} else {
reject('Operation failed.');
}
});
myPromise
.then(result => console.log(result))
.catch(error => console.error(error));