Chain Flow

We’ve hinted at this a couple of times already, but Promises are not just a mechanism for a single-step this-then-that sort of operation. That’s the building block, of course, but it turns out we can string multiple Promises together to represent a sequence of async steps.

The key to making this work is built on two behaviors intrinsic to Promises:

  • Every time you call then(..) on a Promise, it creates and returns a new Promise, which we can chain with.
  • Whatever value you return from the then(..) call’s fulfillment callback (the first parameter) is automatically set as the fulfillment of the chained Promise (from the first point).

Let’s first illustrate what that means, and then we’ll derive how that helps us create async sequences of flow control. Consider the following:

  1. var p = Promise.resolve( 21 );
  2. var p2 = p.then( function(v){
  3. console.log( v ); // 21
  4. // fulfill `p2` with value `42`
  5. return v * 2;
  6. } );
  7. // chain off `p2`
  8. p2.then( function(v){
  9. console.log( v ); // 42
  10. } );

By returning v * 2 (i.e., 42), we fulfill the p2 promise that the first then(..) call created and returned. When p2‘s then(..) call runs, it’s receiving the fulfillment from the return v * 2 statement. Of course, p2.then(..) creates yet another promise, which we could have stored in a p3 variable.

But it’s a little annoying to have to create an intermediate variable p2 (or p3, etc.). Thankfully, we can easily just chain these together:

  1. var p = Promise.resolve( 21 );
  2. p
  3. .then( function(v){
  4. console.log( v ); // 21
  5. // fulfill the chained promise with value `42`
  6. return v * 2;
  7. } )
  8. // here's the chained promise
  9. .then( function(v){
  10. console.log( v ); // 42
  11. } );

So now the first then(..) is the first step in an async sequence, and the second then(..) is the second step. This could keep going for as long as you needed it to extend. Just keep chaining off a previous then(..) with each automatically created Promise.

But there’s something missing here. What if we want step 2 to wait for step 1 to do something asynchronous? We’re using an immediate return statement, which immediately fulfills the chained promise.

The key to making a Promise sequence truly async capable at every step is to recall how Promise.resolve(..) operates when what you pass to it is a Promise or thenable instead of a final value. Promise.resolve(..) directly returns a received genuine Promise, or it unwraps the value of a received thenable — and keeps going recursively while it keeps unwrapping thenables.

The same sort of unwrapping happens if you return a thenable or Promise from the fulfillment (or rejection) handler. Consider:

  1. var p = Promise.resolve( 21 );
  2. p.then( function(v){
  3. console.log( v ); // 21
  4. // create a promise and return it
  5. return new Promise( function(resolve,reject){
  6. // fulfill with value `42`
  7. resolve( v * 2 );
  8. } );
  9. } )
  10. .then( function(v){
  11. console.log( v ); // 42
  12. } );

Even though we wrapped 42 up in a promise that we returned, it still got unwrapped and ended up as the resolution of the chained promise, such that the second then(..) still received 42. If we introduce asynchrony to that wrapping promise, everything still nicely works the same:

  1. var p = Promise.resolve( 21 );
  2. p.then( function(v){
  3. console.log( v ); // 21
  4. // create a promise to return
  5. return new Promise( function(resolve,reject){
  6. // introduce asynchrony!
  7. setTimeout( function(){
  8. // fulfill with value `42`
  9. resolve( v * 2 );
  10. }, 100 );
  11. } );
  12. } )
  13. .then( function(v){
  14. // runs after the 100ms delay in the previous step
  15. console.log( v ); // 42
  16. } );

That’s incredibly powerful! Now we can construct a sequence of however many async steps we want, and each step can delay the next step (or not!), as necessary.

Of course, the value passing from step to step in these examples is optional. If you don’t return an explicit value, an implicit undefined is assumed, and the promises still chain together the same way. Each Promise resolution is thus just a signal to proceed to the next step.

To further the chain illustration, let’s generalize a delay-Promise creation (without resolution messages) into a utility we can reuse for multiple steps:

  1. function delay(time) {
  2. return new Promise( function(resolve,reject){
  3. setTimeout( resolve, time );
  4. } );
  5. }
  6. delay( 100 ) // step 1
  7. .then( function STEP2(){
  8. console.log( "step 2 (after 100ms)" );
  9. return delay( 200 );
  10. } )
  11. .then( function STEP3(){
  12. console.log( "step 3 (after another 200ms)" );
  13. } )
  14. .then( function STEP4(){
  15. console.log( "step 4 (next Job)" );
  16. return delay( 50 );
  17. } )
  18. .then( function STEP5(){
  19. console.log( "step 5 (after another 50ms)" );
  20. } )
  21. ...

Calling delay(200) creates a promise that will fulfill in 200ms, and then we return that from the first then(..) fulfillment callback, which causes the second then(..)‘s promise to wait on that 200ms promise.

Note: As described, technically there are two promises in that interchange: the 200ms-delay promise and the chained promise that the second then(..) chains from. But you may find it easier to mentally combine these two promises together, because the Promise mechanism automatically merges their states for you. In that respect, you could think of return delay(200) as creating a promise that replaces the earlier-returned chained promise.

To be honest, though, sequences of delays with no message passing isn’t a terribly useful example of Promise flow control. Let’s look at a scenario that’s a little more practical.

Instead of timers, let’s consider making Ajax requests:

  1. // assume an `ajax( {url}, {callback} )` utility
  2. // Promise-aware ajax
  3. function request(url) {
  4. return new Promise( function(resolve,reject){
  5. // the `ajax(..)` callback should be our
  6. // promise's `resolve(..)` function
  7. ajax( url, resolve );
  8. } );
  9. }

We first define a request(..) utility that constructs a promise to represent the completion of the ajax(..) call:

  1. request( "http://some.url.1/" )
  2. .then( function(response1){
  3. return request( "http://some.url.2/?v=" + response1 );
  4. } )
  5. .then( function(response2){
  6. console.log( response2 );
  7. } );

Note: Developers commonly encounter situations in which they want to do Promise-aware async flow control with utilities that are not themselves Promise-enabled (like ajax(..) here, which expects a callback). Although the native ES6 Promise mechanism doesn’t automatically solve this pattern for us, practically all Promise libraries do. They usually call this process “lifting” or “promisifying” or some variation thereof. We’ll come back to this technique later.

Using the Promise-returning request(..), we create the first step in our chain implicitly by calling it with the first URL, and chain off that returned promise with the first then(..).

Once response1 comes back, we use that value to construct a second URL, and make a second request(..) call. That second request(..) promise is returned so that the third step in our async flow control waits for that Ajax call to complete. Finally, we print response2 once it returns.

The Promise chain we construct is not only a flow control that expresses a multistep async sequence, but it also acts as a message channel to propagate messages from step to step.

What if something went wrong in one of the steps of the Promise chain? An error/exception is on a per-Promise basis, which means it’s possible to catch such an error at any point in the chain, and that catching acts to sort of “reset” the chain back to normal operation at that point:

  1. // step 1:
  2. request( "http://some.url.1/" )
  3. // step 2:
  4. .then( function(response1){
  5. foo.bar(); // undefined, error!
  6. // never gets here
  7. return request( "http://some.url.2/?v=" + response1 );
  8. } )
  9. // step 3:
  10. .then(
  11. function fulfilled(response2){
  12. // never gets here
  13. },
  14. // rejection handler to catch the error
  15. function rejected(err){
  16. console.log( err ); // `TypeError` from `foo.bar()` error
  17. return 42;
  18. }
  19. )
  20. // step 4:
  21. .then( function(msg){
  22. console.log( msg ); // 42
  23. } );

When the error occurs in step 2, the rejection handler in step 3 catches it. The return value (42 in this snippet), if any, from that rejection handler fulfills the promise for the next step (4), such that the chain is now back in a fulfillment state.

Note: As we discussed earlier, when returning a promise from a fulfillment handler, it’s unwrapped and can delay the next step. That’s also true for returning promises from rejection handlers, such that if the return 42 in step 3 instead returned a promise, that promise could delay step 4. A thrown exception inside either the fulfillment or rejection handler of a then(..) call causes the next (chained) promise to be immediately rejected with that exception.

If you call then(..) on a promise, and you only pass a fulfillment handler to it, an assumed rejection handler is substituted:

  1. var p = new Promise( function(resolve,reject){
  2. reject( "Oops" );
  3. } );
  4. var p2 = p.then(
  5. function fulfilled(){
  6. // never gets here
  7. }
  8. // assumed rejection handler, if omitted or
  9. // any other non-function value passed
  10. // function(err) {
  11. // throw err;
  12. // }
  13. );

As you can see, the assumed rejection handler simply rethrows the error, which ends up forcing p2 (the chained promise) to reject with the same error reason. In essence, this allows the error to continue propagating along a Promise chain until an explicitly defined rejection handler is encountered.

Note: We’ll cover more details of error handling with Promises a little later, because there are other nuanced details to be concerned about.

If a proper valid function is not passed as the fulfillment handler parameter to then(..), there’s also a default handler substituted:

  1. var p = Promise.resolve( 42 );
  2. p.then(
  3. // assumed fulfillment handler, if omitted or
  4. // any other non-function value passed
  5. // function(v) {
  6. // return v;
  7. // }
  8. null,
  9. function rejected(err){
  10. // never gets here
  11. }
  12. );

As you can see, the default fulfillment handler simply passes whatever value it receives along to the next step (Promise).

Note: The then(null,function(err){ .. }) pattern — only handling rejections (if any) but letting fulfillments pass through — has a shortcut in the API: catch(function(err){ .. }). We’ll cover catch(..) more fully in the next section.

Let’s review briefly the intrinsic behaviors of Promises that enable chaining flow control:

  • A then(..) call against one Promise automatically produces a new Promise to return from the call.
  • Inside the fulfillment/rejection handlers, if you return a value or an exception is thrown, the new returned (chainable) Promise is resolved accordingly.
  • If the fulfillment or rejection handler returns a Promise, it is unwrapped, so that whatever its resolution is will become the resolution of the chained Promise returned from the current then(..).

While chaining flow control is helpful, it’s probably most accurate to think of it as a side benefit of how Promises compose (combine) together, rather than the main intent. As we’ve discussed in detail several times already, Promises normalize asynchrony and encapsulate time-dependent value state, and that is what lets us chain them together in this useful way.

Certainly, the sequential expressiveness of the chain (this-then-this-then-this…) is a big improvement over the tangled mess of callbacks as we identified in Chapter 2. But there’s still a fair amount of boilerplate (then(..) and function(){ .. }) to wade through. In the next chapter, we’ll see a significantly nicer pattern for sequential flow control expressivity, with generators.

Terminology: Resolve, Fulfill, and Reject

There’s some slight confusion around the terms “resolve,” “fulfill,” and “reject” that we need to clear up, before you get too much deeper into learning about Promises. Let’s first consider the Promise(..) constructor:

  1. var p = new Promise( function(X,Y){
  2. // X() for fulfillment
  3. // Y() for rejection
  4. } );

As you can see, two callbacks (here labeled X and Y) are provided. The first is usually used to mark the Promise as fulfilled, and the second always marks the Promise as rejected. But what’s the “usually” about, and what does that imply about accurately naming those parameters?

Ultimately, it’s just your user code and the identifier names aren’t interpreted by the engine to mean anything, so it doesn’t technically matter; foo(..) and bar(..) are equally functional. But the words you use can affect not only how you are thinking about the code, but how other developers on your team will think about it. Thinking wrongly about carefully orchestrated async code is almost surely going to be worse than the spaghetti-callback alternatives.

So it actually does kind of matter what you call them.

The second parameter is easy to decide. Almost all literature uses reject(..) as its name, and because that’s exactly (and only!) what it does, that’s a very good choice for the name. I’d strongly recommend you always use reject(..).

But there’s a little more ambiguity around the first parameter, which in Promise literature is often labeled resolve(..). That word is obviously related to “resolution,” which is what’s used across the literature (including this book) to describe setting a final value/state to a Promise. We’ve already used “resolve the Promise” several times to mean either fulfilling or rejecting the Promise.

But if this parameter seems to be used to specifically fulfill the Promise, why shouldn’t we call it fulfill(..) instead of resolve(..) to be more accurate? To answer that question, let’s also take a look at two of the Promise API methods:

  1. var fulfilledPr = Promise.resolve( 42 );
  2. var rejectedPr = Promise.reject( "Oops" );

Promise.resolve(..) creates a Promise that’s resolved to the value given to it. In this example, 42 is a normal, non-Promise, non-thenable value, so the fulfilled promise fulfilledPr is created for the value 42. Promise.reject("Oops") creates the rejected promise rejectedPr for the reason "Oops".

Let’s now illustrate why the word “resolve” (such as in Promise.resolve(..)) is unambiguous and indeed more accurate, if used explicitly in a context that could result in either fulfillment or rejection:

  1. var rejectedTh = {
  2. then: function(resolved,rejected) {
  3. rejected( "Oops" );
  4. }
  5. };
  6. var rejectedPr = Promise.resolve( rejectedTh );

As we discussed earlier in this chapter, Promise.resolve(..) will return a received genuine Promise directly, or unwrap a received thenable. If that thenable unwrapping reveals a rejected state, the Promise returned from Promise.resolve(..) is in fact in that same rejected state.

So Promise.resolve(..) is a good, accurate name for the API method, because it can actually result in either fulfillment or rejection.

The first callback parameter of the Promise(..) constructor will unwrap either a thenable (identically to Promise.resolve(..)) or a genuine Promise:

  1. var rejectedPr = new Promise( function(resolve,reject){
  2. // resolve this promise with a rejected promise
  3. resolve( Promise.reject( "Oops" ) );
  4. } );
  5. rejectedPr.then(
  6. function fulfilled(){
  7. // never gets here
  8. },
  9. function rejected(err){
  10. console.log( err ); // "Oops"
  11. }
  12. );

It should be clear now that resolve(..) is the appropriate name for the first callback parameter of the Promise(..) constructor.

Warning: The previously mentioned reject(..) does not do the unwrapping that resolve(..) does. If you pass a Promise/thenable value to reject(..), that untouched value will be set as the rejection reason. A subsequent rejection handler would receive the actual Promise/thenable you passed to reject(..), not its underlying immediate value.

But now let’s turn our attention to the callbacks provided to then(..). What should they be called (both in literature and in code)? I would suggest fulfilled(..) and rejected(..):

  1. function fulfilled(msg) {
  2. console.log( msg );
  3. }
  4. function rejected(err) {
  5. console.error( err );
  6. }
  7. p.then(
  8. fulfilled,
  9. rejected
  10. );

In the case of the first parameter to then(..), it’s unambiguously always the fulfillment case, so there’s no need for the duality of “resolve” terminology. As a side note, the ES6 specification uses onFulfilled(..) and onRejected(..) to label these two callbacks, so they are accurate terms.