Using promises can be confusing. In this lesson, I show you how to create promises to chain functions together in a specified order. You'll also learn how to pass the return value of promises as the input parameters of promises further down the chain. All examples in this lesson utilize native ES6 style promises, which are fully supported by recent versions of node.js without any dependencies.
I have these four functions here named One, Two, Three, and Four, and I want to execute them in order. I can do that just by calling each one. When I run it, they run in the order that I specified, because each one is returning immediately. What if they didn't return immediately?
What if they were doing something like an API call, where you didn't know how long it was going to take the server being called to return? We can simulate that with a setTimeout, and I can vary the amount of time that each one is taking to return here. Now if we run that, you can see that they didn't return in the order that they were called. I still called them in the order of One, Two, Three, Four, but they returned in the order of Three, Two, One, Four.
This is where promises come into play. We can return a promise, and then that promise will get fulfilled whenever the function has completed. We can use that to control the flow of our application. Let's re-factor it and take a look at how that's going to look.
I'm going to re-factor my function, so that the first thing it does is it returns a new promise. That promise is going to have a parameter called Resolve. Then we're going to have our setTimeout. We can logout the name of the function, again specifying our timeout. After we log this out, we want to call the Resolve parameter, which is actually a function of the promise.
I'm going to do the same thing for the rest of these functions, just modifying the time that it waits and the output of the console.log statement. The premise is still the same. It's just creating a promise, and then, after the timeout, we're resolving that promise.
We need to change the way that we're going to call this to execute, as well. We're still going to call our first function, but because we're using promises now, we now have a Then method. What that's going to do is we call the function, which returns a promise, then when that promise is resolved, we can call function Two, then Three, and then Four.
Let's take a look at running that. Now they're back running in order, One, Two, Three, Four. Let's take this a little step further. I want to give each one of these functions a parameter. We'll call it Ops. In the console.log statement, I'm going to logout that parameter, rather than a hard-coded number. I'm also going to return that whenever I call the Resolve method.
In function Two, I'm going to increment that counter ops, and then again we'll log it out and return it as part of our Resolve function. Same thing for function number three, and ditto for function number four. Now if I supply a parameter to the function named One, and then run that, we get the original output that we're shooting for of One, Two, Three, Four.
Let's take a look and see how that worked. Function One receives a parameter called Ops, and that parameter, when the timeout expired, we logged it out. Then when we called the Resolve method, we returned the Ops parameter. What that does is any value returned by the resolve method gets passed as a parameter to the next function in the chain.
In this case, we called function Two, supplying the results, the return parameter of function One as our parameter. We incremented that counter, which resulted in the number two being output. Then we returned that in our Resolve method and called function Three, which again incremented it, output it, returned as the parameter, and again to function Four.
That's pretty cool when things work right, but what about when things don't work right, when we shouldn't be resolving our promise? Promises actually have a second parameter called Reject, as well. We can check for condition. If that condition is true, we decide we want to reject that promise, we can do so by calling the Reject method.
Again. it has a return value that we can supply that's either an object, or a string of text in this case. Then we'll have our Else, and we'll execute our setTimeout. Then to catch this at the end of our statement here, we're going to use the .catch, which has a parameter we'll call Error, and then we'll just logout our error.
When we run that now, we've called the function One, passed in the parameter of one. The output of function One was the parameter passed in, which was one. That gets passed as a parameter to function Two, where we determine that the Ops variable was equal to one, so we rejected that promise with the message found in error, which then triggered our catch block, output the error, and stopped the promise or stopped the sequence of events.
One last thing to point out on this is that you'll notice I didn't require any NPM modules or any external dependencies. These ES6 style promises are now natively supported by any of the recent versions of node.js, so you can just use them right out of the box.