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

查看:26
本文介绍了处理嵌套 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);
          });
      });

我知道我可以使用像 all()join() 之类的 Promise 函数,但这些函数似乎同时运行,我不能这样做,因为持久化某些表需要来自先前持久化表中的字段.我希望有某种方法可以让我做类似以下的事情,但我似乎无法找到方法:

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. 如果您想按顺序运行一系列承诺,则有 需要注意的重要一点是它不是一系列承诺.这是一个做出承诺的函数数组.这是因为 Promise 会立即执行,因此您无法在需要之前创建 Promise.

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

    现在外部函数只是另一个返回承诺的函数,这意味着你可以应用与内部函数相同的模式.

    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天全站免登陆