使用 bluebird promise.all 两次

Using bluebird promise.all twice

我使用下面的代码,效果不错!我的问题是是否有更好的方法来编写它或者可以吗?我使用了 promise 两次,但想验证我是否正确使用了这个 promise 库

var start = function () {
    return Promise.all([
        chi.getCommand(val1,val2),
        chi.findAndUpdateCustomer()
    ]).then(function (args) {
        return Promise.all([chss.exe(runnableDoc, args[0], args[1]),
            Promise.delay(10).then(function (val) {
                val ? console.log(val) : null;
                return app.getStatus(12)
            })
        ])
    })
}();

想法:

  • 我更愿意使用 .spread() 而不是 .then() 顺序到 Promise.all() 在这里,它使使用正确的变量名称更容易。
  • 我不确定封闭的 IIFE 应该实现什么,让我们摆脱它。
  • 在这种特殊情况下,第二个 Promise.all() 对我来说似乎是多余的。

所以我的建议是:

var start = Promise.all([
    chi.getCommand(val1, val2),
    chi.findAndUpdateCustomer()
]).spread(function (command, customer) {
    return chss.exe(runnableDoc, command, customer).delay(10).then(function (val) {
        if (val) console.log(val);
        return app.getStatus(12);
    });
});

备注

  • 尝试使用比 start 更好的名称,即 sth。表示操作的结果,而不是您已经开始操作的无关事实。
  • 10 毫秒的延迟应该做什么?我认为没有必要。
  • 在上面的示例中,val 将引用 chss.exe() 调用的结果。但是,在您的设置中,val 将始终是 undefined;我想这不是本意。
  • 一个成功的 promise 链的最终结果总是 app.getStatus(12)。但如果出现错误,结果将不明确。为了对称起见,您应该添加一个 .catch(),returns 一个不同的应用程序状态。