Promises in JavaScript
- A Promise is an object representing the eventual completion or failure of an asynchronous operation.
States of a Promise:
Pending
: The initial state, neither fulfilled nor rejected.Fulfilled
: The operation was completed successfully.Rejected
: The operation failed.
let promise = new Promise((resolve, reject) => { let success = true; if (success) { resolve('Operation successful'); } else { reject('Operation failed'); }});
promise.then((message) => { console.log(message);}).catch((error) => { console.error(error);});