Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS的连锁承诺_Javascript_Angularjs_Promise_Chaining - Fatal编程技术网

Javascript AngularJS的连锁承诺

Javascript AngularJS的连锁承诺,javascript,angularjs,promise,chaining,Javascript,Angularjs,Promise,Chaining,我有一个叫做paymentStrategy的服务,它被注入到我的控制器中 $scope.buy = function() { paymentStrategy.buy() .then(function(response) { } } paymentStrategy的这个buy方法触发了几个需要顺序调用的方法。完成buy()中的所有方法后,需要调用() 这可能是微不足道的,但我是一个新的角度 目前,buy().then()直接在init()方法之后触发。 我觉得我们需要将所有这些

我有一个叫做paymentStrategy的服务,它被注入到我的控制器中

$scope.buy = function() {
  paymentStrategy.buy()
    .then(function(response) {

  }
}
paymentStrategy的这个buy方法触发了几个需要顺序调用的方法。完成buy()中的所有方法后,需要调用()

这可能是微不足道的,但我是一个新的角度

目前,buy().then()直接在init()方法之后触发。 我觉得我们需要将所有这些方法放在一系列承诺中,并应用$q.all()

如有任何帮助或建议,将不胜感激

angular.module('deps-app.payment.services', []).
  factory('paymentStrategy', function($q) {

 var deferred = $q.defer();
 var ITEM_TO_PURCHASE = "test.beer.managed";
 var promises = [];

 var handlerSuccess = function(result) {
      deferred.resolve(result);
  };

 var handlerError = function(result) {
      deferred.reject(result);
  };

 _init = function() {

     inappbilling.init(handlerSuccess, handlerError, { showLog:true }); 
     return deferred.promise;
    }

  _purchase = function() {
        inappbilling.buy(handlerSuccess, handlerError, ITEM_TO_PURCHASE);
        return deferred.promise;
  }

  _consume = function() {
        inappbilling.consumePurchase(handlerSuccess, handlerError, ITEM_TO_PURCHASE);
        return deferred.promise;
  }

return  {

     buy: function() {

      _init();
        .then(_purchase());
        .then(_consume());  

      return deferred.promise;                    
    }

 }
});

如果需要按顺序链接承诺,只需将承诺从一个返回到另一个:

callFirst()
.then(function(firstResult){
   return callSecond();
})
.then(function(secondResult){
   return callThird();
})
.then(function(thirdResult){
   //Finally do something with promise, or even return this
});
如果要将所有这些作为API返回:

function myMethod(){
   //Return the promise of the entire chain
   return first()
           .then(function(){
               return second();
           }).promise;
}

通过添加自己的承诺,使所有方法都成为atomar。在您的代码中,第一个
resolve
将完成整个请求

如果这些方法有自己的承诺,你可以轻松地将它们链接起来

angular.module('deps-app.payment.services', []).factory('paymentStrategy', function($q) {
var ITEM_TO_PURCHASE = "test.beer.managed";

_init = function() {
  return $q(function (resolve, reject) {
    inappbilling.init(resolve, reject, { showLog: true }); 
  });
};

_purchase = function() {
  return $q(function (resolve, reject) {
    inappbilling.buy(resolve, reject, ITEM_TO_PURCHASE);  
  });
};

_consume = function() {
  return $q(function (resolve, reject) {
    inappbilling.consumePurchase(resolve, reject, ITEM_TO_PURCHASE);
  });
};

return  {
  // In this case, you don't need to define a additional promise, 
  // because placing a return in front of the _init, will already return 
  // the promise of _consume.
  buy: function() {    
    return _init()
      .then(_purchase)  
      // remove () from inside the callback, to pass the actual method 
      // instead the result of the invoked method.
      .then(_consume);      
  }    
};

}))

所有关于inappbilling的方法,如init、buy和ConsumerPurchase return承诺吗?听起来不错,但你能澄清一下方法吗?这对你来说是个问题。代码的问题在于,您在init callback和其他回调上解析了承诺,但在调用resolve之前,您需要等待所有调用完成。handlerSuccess和handlerError这两个参数是在方法(init、buy和ConsumerPurchase)完成时调用的回调。i、 e,_purchase()只有在从init(…)调用handlerSuccess时才需要调用_只有在调用handlerSuccess from purchase(…)时才需要调用consume。非常感谢您的帮助,非常感谢。在接受之前先问一个问题。它似乎从来没有在buy()方法中加入过。这样做好吗_init()。然后(_consumerall)。然后(_purchase)。然后(_consumer)。然后(deferred.resolve)啊我的错误。现在使用
$q.defer()
检查is,这也不例外。这三个函数中的每一个都可以通过执行类似于
\u init=function(){return$q(function(resolve,reject){inappbilling.init(resolve,reject,{showLog:true});}的操作来简化将根据需要返回承诺。理想情况下,
inappbilling
上的方法应该返回承诺(仅由您的_init等方法返回),但我认为这可能不是您的决定。@Sean the Bean是对的。@2014年这对我来说是唯一正确的答案,直到我发现了
return$q.
例程。我对承诺也是新手。这里,first-then-return调用second-then,但它是返回方法,实际上second-then将值作为参数。我只知道C,所以我不确定。如此简单,如此强大!非常感谢。只有在读了你的帖子之后,我才明白我必须做什么。我发现的所有其他例子都非常复杂。这是正确的方法。“返回”是必需的,否则将不起作用。