Before the promise we use callBack function for asynchronous Task. which has two Major Issue
1.) Callback Hell (Pyramid of doom)
2.) Inversion of control
Inversion of control is overcome by using promise.
What is the Promise ?
A promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
It Has Three States ๐
1.Pending State : The initial state, neither fulfilled nor rejected.
2.Fulfilled State : Operation Completed Successfully
3.Rejected State : Operation rejected
Key Points ๐ :
In a promise, we do not pass a function directly; instead, we attach a callback function to the promise. When the promise is settled, JavaScript guarantees that the callback function will be called automatically exactly once.
As soon as the promise is fulfilled/rejected , It updates the empty object which is assigned undefined in the pending state.
A promise resolves only once and it is immutable.
A promise provides a method, .then(), to handle the result of the asynchronous operation once it is settled. (fulfilled or rejected)
To avoid callback hell (Pyramid of doom) , We use promise chaining. This way our code expands vertically instead of horizontally.
.then() Method is used For Promises Chaining
In promise chaining, always use the return statement to pass a value from one promise to the next.
Promises provide a way to handle asynchronous tasks, allowing for more readable and maintainable code when dealing with operations that take time to complete, such as network requests, file reading, or database queries.
Promise Api
Promise.all() API ๐
Promise.all() is a powerful method in JavaScript that allows you to handle multiple promises at the same time.
Key Points ๐
- Takes an Iterable Object: Promise.all takes iterable promises ( such as an array ) and returns a single promise.
- Resolves Together: The returned promise resolves when all promises in the array have resolved.
- Results Array: The resolved value is an array of results, in the same order as the original promises.
- Error Handling: If any promise rejects, Promise.all immediately rejects with that error, and it won't wait for other promises to finish.
Promise.allSettled() API ๐
Key Points ๐
- Takes an Iterable Object: Promise.allSettled takes an iterable object (such as an array) and returns a single promise.
- Settles Together: The returned promise settles (resolves or rejects) when all promises in the iterable have settled (either resolved or rejected).
- Results Array: The settled value is an array of result objects, in the same order as the original promises:
- Each Settled result object contains a status property :
- status: "fulfilled" if the promise was successfully resolved, along with a value property containing the resolved value.
- status: "rejected" if the promise was rejected, along with a reason property containing the rejection reason.
- No Interruption: Promise.allSettled waits for all promises to settle (resolve or reject).
Promise.race() API ๐
Promise.race() is a powerful method in JavaScript that allows you to handle multiple promises. It is crucial for scenarios where you need to act on the first settled (resolved or rejected) promise.
Key Points ๐
- Promise.race takes iterable promises ( such as an array ) .
- It returns a single promise with First Settled (resolve or reject ) promise with the First resolved value or the First reason of rejection.
- it does not wait for the other promises to settle.
Promise.any() API ๐
Promise.any() is a powerful method in JavaScript that allows you to handle multiple promises.
Key Points ๐
- Promise.any() takes iterable promises ( such as an array ) .
- It returns a single promise with First Resolved ( success ) promise with the value
- It does wait for promises to resolve but resolves as soon as one of them resolves. It does not wait for all promises to resolve or reject before returning.
- If all promises in the iterable are rejected (none of them resolve successfully), Promise.any() returns a AggregateError which contains an array of all the rejection reasons from the original iterable.