处理嵌套Promise的最佳方法(bluebird) [英] Best way to handle nested Promises (bluebird)

查看:122
本文介绍了处理嵌套Promise的最佳方法(bluebird)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有以下承诺链,它看起来很混乱(每个 _create *函数返回一个承诺):

I have the following promise chain below and it appears to be quite messy (Each _create* function returns a promise):

return new Promise(function (resolve, reject) {
      _this.database.transaction(function (t) {
        _this._createExternalAccount(payment, t)
          .then(function (externalAccount) {
            return _this._createExternalTransaction(externalAccount, payment, t)
              .then(function (externalTransaction) {
                return _this._createAddress(externalAccount, payment, t)
                  .then(function (address) {
                    return _this._createTransaction(address, payment, t)
                      .then(function (transaction) {
                        return _this._createGatewayTransaction(externalTransaction, transaction, payment, t)
                          .then(function (gatewayTransaction) {
                            t.commit();
                            resolve(bridgePayment);
                          });
                      });
                  });
              });
          })
          .error(function (bridgePayment) {
            t.rollback();
            reject(bridgePayment);
          });
      });

我知道我可以使用Promise函数,例如 all() join()但这些似乎同时运行我无法做的函数,因为持久化到某些表需要来自先前持久化表的字段。我希望有一些方法可以让我做类似以下的事情,但我似乎无法弄清楚:

I know there are Promise functions I can use like all() and join() but these seem to run the functions concurrently which I can't do because persisting to some tables require fields from the previously persisted tables. I'm hoping there is some way for me to do something like the following but I can't seem to find out how:

Promise.all(_this._createExternalAccount(payment, t), _this._createExternalTransaction(externalAccount, payment, t), _this._createAddress(externalAccount, payment, t))
    .then(function(externalAccount, externalTransaction, address) {
        // do logic
    });


推荐答案

我很确定你在问什么。


  1. 如果你想按顺序运行一系列承诺,那么这个答案

重要的是要请注意,这不是一系列承诺。这是一系列承诺的功能。那是因为promises会立即执行,所以在你需要之前你不能创建承诺。

The important thing to note is it's not an array of promises. It's an array of functions that make a promise. That's because promises execute immediately so you can't create the promise until you need it.

如果你不想把它们放在数组中,虽然正常事情就是把它们正常地链接起来。

If you don't want to put them in array though the normal thing is just chain them normally.

再一次,最简单的方法就是回归承诺。然后你只需

Again the easiest way to to make a bunch functions the return promises. Then you just

var p = firstFunctionThatReturnsAPromise()
.then(secondFunctionThatReturnsAPromise)
.then(thirdFunctionThatReturnsAPromise)
.then(fourthFunctionThatReturnsAPromise)

你可以轻松地嵌套它们

function AOuterFunctionThatReturnsAPromise() {         
    var p = firstFunctionThatReturnsAPromise()
            .then(secondFunctionThatReturnsAPromise)
            .then(thirdFunctionThatReturnsAPromise)
            .then(fourthFunctionThatReturnsAPromise);
    return p;
};

现在外部函数只是另一个返回promise的函数,这意味着
可以应用同样的模式作为内部函数。

Now that outer function is just another function returning a promise which means you can apply same pattern as the inner functions.

如果当然可以内联

var p = function() {
  return new Promise(resolve, reject) {
    DoSomethingAsync(function(err, result) {
      if (err) {
        reject();
      } else {
        resolve(result);
    };
  };
}).then(function() {
  return new Promise(resolve, reject) {
    DoSomethingAsync(function(err, result) {
      if (err) {
        reject(err);
      } else {
        resolve(result);
    };
  };
}).then(function() {
  var err = DoSomethingNotAsync();
  if (err) {
     return Promise.reject(err);
  } else {
     return Promise.resolve();
  }
});

等......

这篇关于处理嵌套Promise的最佳方法(bluebird)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆