Introduction to Promises
A Promise is an object that represents the eventual completion or failure of an asynchronous operation. Introduced in ES2015, promises provide a cleaner, more composable alternative to callback-based async code.
Promise States
A promise exists in one of three states: pending (initial, neither fulfilled nor rejected), fulfilled (operation completed successfully), or rejected (operation failed). Once settled (fulfilled or rejected) a promise cannot change state.
then, catch, finally
Chain .then(onFulfilled) to handle success, .catch(onRejected) to handle errors, and .finally(fn) to run cleanup code regardless of outcome. Each .then() returns a new promise, enabling method chaining.
Creating Promises
Use the Promise constructor with an executor function that receives resolve and reject callbacks. Call resolve(value) on success and reject(reason) on failure.