Professional Documents
Culture Documents
Promises in JS
Promises in JS
Promises are a JavaScript feature that provides a more structured and readable
way to work with asynchronous code. They represent the eventual completion or
failure of an asynchronous operation, allowing you to handle the result or error in
a more organized and manageable manner.
2. States:
Rejected: There was an error during the operation, and the promise
has a reason for the failure.
3. Chaining:
4. Error Handling:
Promises have built-in error handling through the catch method, making it
easier to manage and propagate errors in asynchronous code.
Promises in JS 1
1. Avoiding Callback Hell (Callback Pyramids):
// Without Promises
asyncOperation1((result1) => {
asyncOperation2(result1, (result2) => {
asyncOperation3(result2, (result3) => {
// ...
});
});
});
// With Promises
asyncOperation1()
.then((result1) => asyncOperation2(result1))
.then((result2) => asyncOperation3(result2))
.then((result3) => {
// ...
});
// Without Promises
asyncOperation1((result1) => {
asyncOperation2(result1, (result2) => {
asyncOperation3(result2, (result3) => {
// ...
});
});
Promises in JS 2
});
// With Promises
asyncOperation1()
.then((result1) => asyncOperation2(result1))
.then((result2) => asyncOperation3(result2))
.then((result3) => {
// ...
});
3. Error Handling:
asyncOperation1()
.then((result1) => asyncOperation2(result1))
.then((result2) => asyncOperation3(result2))
.catch((error) => {
console.error('An error occurred:', error);
});
Promise.all([promise1, promise2])
.then((results) => {
Promises in JS 3
const result1 = results[0];
const result2 = results[1];
// ...
})
.catch((error) => {
console.error('An error occurred:', error);
});
In summary, promises provide a cleaner and more organized way to work with
asynchronous code, making it easier to read, write, and maintain. They address
common challenges associated with callback-based code and promote better
error handling and sequential execution of asynchronous operations.
Promises Basics:
1. Creating a Promise:
The Promise constructor takes a function with two parameters: resolve and
reject .
2. Resolving a Promise:
Promises in JS 4
const successfulPromise = new Promise((resolve, reject) =>
{
setTimeout(() => {
resolve('Operation succeeded!');
}, 1000);
});
3. Rejecting a Promise:
Use the reject function when there's an error during the asynchronous
operation.
Consuming Promises:
1. Using then and catch :
successfulPromise
.then((result) => {
console.log(result); // Output: Operation succeeded!
})
.catch((error) => {
console.error(error); // This won't be called in this
Promises in JS 5
example
});
2. Chaining Promises:
Promises can be chained using then . Each then returns a new promise.
successfulPromise
.then((result) => {
console.log(result); // Output: Operation succeeded!
return 'New value';
})
.then((newValue) => {
console.log(newValue); // Output: New value
})
.catch((error) => {
console.error(error);
});
3. Promise All:
Promise.all([promise1, promise2])
.then((values) => {
console.log(values); // Output: ['One', 'Two']
})
.catch((error) => {
console.error(error);
Promises in JS 6
});
Promises are essential for handling asynchronous code in a clean and readable
way, especially when working with features like fetching data from a server,
handling events, or working with timers.
Promises in JS 7