They are similar to real life promise, how we make promise to someone that i am making promise that i will come meet you, same way we are just telling that, this piece code will get executed in future.
Promise object in javascript have two main part, “producing code” and “consuming code”, producing code’s job is to produce the code, and consuming code’s job is tot consume the results from the produced code.
function passed to new Promise is called executor, which will execute them and then will make results for consuming code to consume the result.
so in short, executor runs automatically and attempts to do the job, when it finished with the task, it calls resolve if it was successful or reject if there was an error.
Promise Object have this these two internal properties:
resolve example:
state: “fulfilled”,
result: “done”
reject example:
state: “rejected”,
result: “error”
there can be only single result or an error
Consumer code:
.then part, when promises is either resolved or rejected, to get both values we use .then method on the promise object to get our result or error
error example:
just like normal try catch block, we have same with promises also we have try to get desired result, catch to catch the error, and finally block to execute final code when every thing is done!
catch:
finally block don’t care about the result of promise, whether it was fulfilled or un fulfilled, it only knows to execute the code it is provided, and order matters in finally, if we write finally before the catch or then, it will run first,
what will be result ?
difference between callback and promise ?
callback can be only called once, there is result or some kind of trigger, promises can be called as many times you want even after promise is fulfilled to access the result as many times you want
chaining in promises:
we can chain promises, example:
adding many then don’t means chaining, they don’t forward the result to the next thing, they act as independent promise
Promise Apis:
There are 6 static methods of Promise class:
Promise.all:
we want to execute many promise in parallel and want to wait until all of them are resolved
the order of results will be same as the order of promise, even if the promise got rejected
A popular trick is to map the array of job and pass them into promise all
important thing to keep in mind about promise.all
If any of the promises is rejected, the promise returned by Promise.all immediately rejects with that error.
example of error:
also promise.all not cancel and does nothing about existing ongoing request, even if promise already got rejected, it will simply no longer watch them or care them, promise got rejected then it’s over, promise.all no longer cares about any thing.
in short,
Promise.all = all or nothing
Promise.allSettled:
this one is recently added, old browsers don’t support, they need polyfills and transpilors
Promise.allSettled just waits for all promises to settle, regardless of the result.
{status:"fulfilled", value:result} for successful responses,
{status:"rejected", reason:error} for errors.
polyfill implementation for incompatible browsers:
Promise.race:
it’s like fastest finger first, whichever gets resolved first wins
promise.any:
promise any just does what it tells, if any of the promise gets fulfilled, then it returns the result, then ignores the others, if all get rejected, then it combines the error(AggregateError) and returns it
Promise.resolve/reject:
they are not used that much, because of async/await
Promise.resolve(value) – makes a resolved promise with the given value.
Promise.reject(error) – makes a rejected promise with the given error.
Promise handlers .then/.catch/.finally are always asynchronous.
what does that mean:
put it more simply, when a promise is ready, its .then/catch/finally handlers are put into the queue; they are not executed yet. When the JavaScript engine becomes free from the current code, it takes a task from the queue and executes it.
That’s why “code finished” in the example above shows first.
internal implementation if you are interested of apis of promise:
Promise.race:
Promise.all:
Promise.allSettled:
Promise.any:
That’s it for now, will add more things when i learn more!