3

I use Bluebird Promises for a Node.js application. How can I introduce conditional chain branches for my application? Example:

exports.SomeMethod = function(req, res) {
        library1.step1(param) 
        .then(function(response) { 
            //foo

            library2.step2(param)
            .then(function(response2) { //-> value of response2 decides over a series of subsequent actions
                if (response2 == "option1") {
                    //enter nested promise chain here?
                    //do().then().then() ...
                }

                if (response2 == "option2") {
                    //enter different nested promise chain here?
                    //do().then().then() ...
                }

               [...]
            }).catch(function(e) { 
                //foo
            });
    });
};

Apart from not having figured out a working version of this yet, this solution feels (and looks) weird somehow. I got a sneaking suspicion that I am somewhat violating the concept of promises or something like that. Any other suggestions how to introduce this kind of conditional branching (each featuring not one but many subsequent steps)?

Igor P.
  • 1,407
  • 2
  • 20
  • 34
  • See also [nested](http://stackoverflow.com/a/22000931/1048572) and [conditional](http://stackoverflow.com/q/26599798/1048572) chains in general – Bergi Jun 16 '15 at 21:55

2 Answers2

4

Yes, you can do it, just like that. The important thing is just to always return a promise from your (callback) functions.

exports.SomeMethod = function(req, res) {
    return library1.step1(param)
//  ^^^^^^
    .then(function(response) { 
        … foo

        return library2.step2(param)
//      ^^^^^^
        .then(function(response2) {
            if (response2 == "option1") {
                // enter nested promise chain here!
                return do().then(…).then(…)
//              ^^^^^^
            } else if (response2 == "option2") {
                // enter different nested promise chain here!
                return do().then(…).then(…)
//              ^^^^^^
            }
        }).catch(function(e) { 
            // catches error from step2() and from either conditional nested chain
            …
        });
    }); // resolves with a promise for the result of either chain or from the handled error
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Just return additional promises from within your .then() handler like I show below. The key is to return a promise from within a .then() handler and that automatically chains it into the existing promises.

exports.SomeMethod = function(req, res) {
        library1.step1(param) 
        .then(function(response) { 
            //foo

            library2.step2(param)
            .then(function(response2) { //-> value of response2 decides over a series of subsequent actions
                if (response2 == "option1") {
                    // return additional promise to insert it into the chain
                    return do().then(...).then(...);
                } else if (response2 == "option2") {
                    // return additional promise to insert it into the chain
                    return do2().then(...).then(...);
                }

               [...]
            }).catch(function(e) { 
                //foo
            });
    });
};
jfriend00
  • 683,504
  • 96
  • 985
  • 979