【问题标题】:Bluebird Promise: Nested or conditional chainsBluebird Promise:嵌套或条件链
【发布时间】:2015-06-17 05:42:34
【问题描述】:

我将 Bluebird Promises 用于 Node.js 应用程序。如何为我的应用程序引入条件链分支?示例:

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

除了还没有找到一个可行的版本之外,这个解决方案在某种程度上感觉(和看起来)很奇怪。我偷偷怀疑我在某种程度上违反了承诺或类似的概念。还有其他关于如何引入这种条件分支的建议(每个步骤都不是一个而是多个后续步骤)?

【问题讨论】:

标签: javascript promise bluebird


【解决方案1】:

是的,你可以做到,就像那样。重要的是从您的(回调)函数中始终return 一个承诺

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

【讨论】:

    【解决方案2】:

    只需从您的.then() 处理程序中返回额外的承诺,如下所示。关键是从 .then() 处理程序中返回一个承诺,并自动将其链接到现有的承诺中。

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

    【讨论】:

      猜你喜欢
      • 2017-01-16
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多