Igor P.
Igor P.

Reputation: 1477

Bluebird Promise: Nested or conditional chains

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)?

Upvotes: 3

Views: 3928

Answers (2)

Bergi
Bergi

Reputation: 664346

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
};

Upvotes: 4

jfriend00
jfriend00

Reputation: 707228

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
            });
    });
};

Upvotes: 0

Related Questions