Background
I am really new to nodejs and javascript for that matter. I have used these in past but mainly to manipulate DOM elements, post forms , handle event etc. We saw in last post on node.js working how node.js works on single threaded, non-blocking and asynchronous programming model.
This is achieved mainly using events. So lets say you want to read a file on the file system. You make a call to access this file and essentially provide a callback function which will be invoked on successful completion of file access and meanwhile program can carry on with it's next set of function.
Now sometime we do need to synchronise tasks. For example lets say we want to complete reading files json content which is required to make a network call. One possible way is to do the network call in the callback function of the file access function so that when we get a callback on file access completion we read the file and then make a network call.
This sounds simple enough for a single function to be synchronised. Try to imagine multiple functions that need to be synchronised. It can be a nightmare coding that - writing new function in each functions callback (cascading it).
Understanding Promises in javascript
It is literally what it means - a promise. It is an object that may produce a result in the future. Think of it like a Future object in Java (I come from a Java background, hence the reference. Ignore if you are not aware) -
A promise of a function can be in either one of the following state -
- Fulfilled : Asynchronous operation corresponding to this promise is completed successfully.
- Rejected : Asynchronous operation corresponding to this promise has failed. Promise will have the reason why it failed.
- Pending : The asynchronous operation is still pending and is neither in fulfilled or rejected state.
- Settled : This is a generic state. Asynchronous operation is complete and can be in - Fulfilled or Rejected state.
A state will be in pending till the asynchronous operation is in progress. Once it is complete state can be fulfilled or rejected and from then state cannot change.
NOTE : We are saying asynchronous operation because general processed are asynchronous for which promise are created. But it need not be. Promise may correspond to a synchronous operation as well.
Consider a simple promise as follows -
var testPromise = new Promise(function(resolve, reject){ //your test operation - can be async let testSuccess = true; // can be false depending on if your test async operation failed if(testSuccess) { resolve("success"); } else { reject("failure"); } }); testPromise.then(function(successResult){ console.log("Test promise succeded with result : " + successResult); }).catch(function(failureResult){ console.log("Test promise failed with result : " + failureResult);
This prints output : Test promise succeded with result : success
You can change testSuccess to false and it will print : Test promise failed with result : failure
So let's see what happened here.
- First we created a new promise with constructor new Promise()
- constructor takes an argument as function that basically defines what operation needs to be performed as part of that promise
- This function takes two callbacks -
- resolve()
- reject()
- You will call resolve() when your operation is successful and will call reject when it fails. resolve() will essentially put the promise in Fulfilled state where as reject will put it in Rejected state ww saw above.
- Depending on result of our operation (can be asynchronous) we will call resolve() or reject()
- Once promise object is created we can call it using then() method of promise object. then() method will be called when promise is fulfilled and catch() method will be called when it is rejected/failed.
- You can toggle the value of testSuccess boolean and see for yourself.
- Each then() and catch() take an argument which is nothing but variable passed by resolve() and reject() which in this case is success or failure
Now that we know what promise is and how it behaves lets see if this can solve our synchronisation problem. We have 3 asynchronous operations and we need to do it one after the other -
var test1 = new Promise(function(resolve,reject){ resolve('test1'); }); var test2 = new Promise(function(resolve,reject){ resolve('test2'); }); var test3 = new Promise(function(resolve,reject){ resolve('test3'); }); test1.then(function(test1Result){ console.log('completed : ' + test1Result); return test2; }).then(function(test2Result){ console.log('completed : ' + test2Result); return test3; }).then(function(test3Result){ console.log('completed : ' + test3Result); });
This one outputs -
completed : test1
completed : test2
completed : test3Only difference here is in each then function we are returning next promise and calling then on it so that it is run sequentially.
Alternatively you can also do -
var test1Func = function() { return test1; }; var test2Func = function() { return test2; }; var test3Func = function() { return test1; }; test1Func().then(function(test1Result){ console.log('completed : ' + test1Result); return test2Func(); }).then(function(test2Result){ console.log('completed : ' + test2Result); return test3Func(); }).then(function(test3Result){ console.log('completed : ' + test3Result); });
Promise.all([test1Func(),test2Func(),test3Func()]).then(function(){ console.log('All tests finished'); });
And this will output - All tests finished
Similarly if you want to do something if any one of the promise is complete you can do -
Promise.race([test1Func(),test2Func(),test3Func()]).then(function(){ console.log('All tests finished'); });
and this will output - One of the tests finished
To sum it up promise looks like below -
No comments:
Post a Comment