JS Promises
const promise = new Promise((resolve, reject) => {
// resolve(value) or reject(error)
});
promise.then(value => {}).catch(error => {}).finally(() => {});
What Is a Promise
A Promise is an object representing the eventual result of an operation that hasn't finished yet, starting in a pending state, and eventually settling into either fulfilled, meaning it succeeded, or rejected, meaning it failed.
- Think of a promise's three states as a package's shipping status:
- pending means still in transit
- fulfilled means delivered
- rejected means lost.
उदाहरण: What Is a Promise
const promise = new Promise((resolve, reject) => {
resolve("Success!");
});
console.log(promise); // Promise {<fulfilled>: 'Success!'}
then, catch, and finally
.then() runs a function when a promise successfully fulfills, .catch() runs a function when a promise rejects, and .finally() runs a function regardless of whether the promise succeeded or failed.
उदाहरण: then, catch, and finally
// Declare the constant `promise` as a new `Promise` instance
// Declare the constant `promise` as a new `Promise` instance
const promise = new Promise((resolve) => resolve("Done"));
promise
// On success, run this with the resolved value as `result`
// On success, run this with the resolved value as `result`
.then(result => console.log(result))
// On failure, run this with the error as `err`
// On failure, run this with the error as `err`
.catch(err => console.log(err))
.finally(() => console.log("Finished"));
Chaining Promises
Multiple .then() calls can be chained together, with each one receiving the value returned by the previous one, letting you express a sequence of asynchronous steps in a clean, readable, top-to-bottom order.
उदाहरण: Chaining Promises
Promise.resolve(1)
.then(n => n + 1)
.then(n => n * 2)
.then(result => console.log(result)); // 4
Promise.all
Promise.all() takes an array of promises and returns a single new promise that fulfills once every one of them has fulfilled, with an array of all their results, or rejects immediately if any single one rejects.
उदाहरण: Promise.all
Promise.all([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then(results => console.log(results)); // [1, 2, 3]
Promise.race
Promise.race() takes an array of promises and settles as soon as the very first one settles, whether it fulfills or rejects, effectively racing them against each other and returning the fastest outcome.
उदाहरण: Promise.race
Promise.race([
new Promise(resolve => setTimeout(() => resolve("slow"), 100)),
new Promise(resolve => setTimeout(() => resolve("fast"), 10)),
]).then(result => console.log(result)); // "fast"
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic