Promise API with parallel execution ??
Promise API with parallel execution ??
RUNNING MULTIPLE
PROMISES IN PARALLEL
JAVASCRIPT INTERVIEW
SPECIAL
@subhajit-adhikary
001
What are Promises??
A promise is an object that will produce a single value sometime in the
future. If the promise is successful, it will produce a resolved value, but if
something goes wrong then it will produce a reason why the promise failed.
Simply said:- It behaves very much similar to real life promises.
Output
@subhajit-adhikary
002
why parallel execution
of Promises??
There will be times when we have multiple promises and we want to execute
them.
One way will be to execute them one after the other, but this seems redundant
if the returned value of one promise is not used in next promises.
If the promises are independent of each other, we can save a lot of time
running them in parallel.
@subhajit-adhikary
003
Promise Api - what
methods we have ??
The parallel methods in Promise API runs the promises parallely taking
lesser time as compared to sequential execution.
We will discuss about 4 such methods and also see how to implement them
on our own
Common Code
Promise.all()
Promise.allSettled()
Promise.any()
Promise.race()
@subhajit-adhikary
004
Promise.all()
It takes an array of promises as input and returns a single Promise.
The returned promise fulfills when all of the input's promises fulfill
It rejects when any of the input's promise rejects, with the first rejection reason.
The array of resolved values have same order as promises passed
Native Promise.all()
Custom Promise.all()
@subhajit-adhikary
005
Promise.allSettled()
It takes an array of promises as input and returns a single Promise.
The returned promise fulfills when all of the input's promises settle (resolve or
reject ).
The array of settled values have same order as promises passed
Native Promise.allSettled()
Custom
Promise.allSettled()
Output
@subhajit-adhikary
006 Promise.any()
It takes an array of promises as input and returns a single Promise.
The returned promise fulfills when any of the input's promises fulfill
It rejects when all of the input's promises reject, with an AggregateError
containing an array of rejection reasons.
We will get the value of first fullfilled promise
Native Promise.any()
Custom Promise.any()
@subhajit-adhikary
007
Promise.race()
It takes an array of promises as input and returns a single Promise.
The returned promise fulfills when any of the input's promises settle (resolve or
reject ).
We will get the value of first settled promise.
Native Promise.race()
Custom Promise.race()
@subhajit-adhikary