Promises
A promise is an object which can be returned synchronously from an asynchronous function (ref).
Promises can be used to avoid callback hell, and they are more and more frequently encountered in modern JavaScript projects.
A Promise is an object with 3 main properties value
and 2 hidden arrays onFulfillment
and onRejection
. Any function in these arrays will automatically be run with value as an argument.
.then
method is adding function toonFulfillment
..catch
method is adding function toonRejection
.
Depending on if we face an error or not, different function will be invoked.
Function in the
onFulfillment
andonRejection
array don't go in the regular callback queue, they enter their own microtask queue.
Sample code
Explanation
When you do an Ajax request the response is not synchronous because you want a resource that takes some time to come. It even may never come if the resource you have requested is unavailable for some reason (404).
To handle that kind of situation, ES2015 has given us promises. Promises can have three different states:
- Pending
- Fulfilled
- Rejected
Let's say we want to use promises to handle an Ajax request to fetch the resource X.
Create the promise
We firstly are going to create a promise. We will use the jQuery get method to do our Ajax request to X.
As seen in the above sample, the Promise object takes an executor function which takes two parameters resolve and reject. Those parameters are functions which when called are going to move the promise pending state to respectively a fulfilled and rejected state.
The promise is in pending state after instance creation and its executor function is executed immediately. Once one of the function resolve or reject is called in the executor function, the promise will call its associated handlers.
Promise handlers usage
To get the promise result (or error), we must attach to it handlers by doing the following:
If the promise succeeds, resolve is executed and the function passed as .then
parameter is executed.
If it fails, reject is executed and the function passed as .catch
parameter is executed.
Note : If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached. (Ref: MDN)