简体   繁体   English

与AngularJS的链承诺

[英]Chain promises with AngularJS

I have a service called paymentStrategy that get injected in my controller. 我有一个名为paymentStrategy的服务,它被注入我的控制器。

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

  }
}

This buy method from paymentStrategy triggers several methods that needs to be called sequentially. 来自paymentStrategy的这种购买方法会触发需要按顺序调用的几种方法。 When all the methods within buy() are done, then() needs to be called. 当buy()中的所有方法都完成后,需要调用()。

It is probably trivial but I am quite new to angular. 这可能是微不足道的,但我对角度很新。

At the moment, buy().then() gets triggered straight after the init() methods. 目前,在init()方法之后直接触发buy()。then()。 I have the feeling we need to put all theses methods in a array of promises and apply $q.all(). 我觉得我们需要将所有这些方法放在一个promises数组中并应用$ q.all()。

Any help or suggestion would be greatly appreciated 任何帮助或建议将不胜感激

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

 }
});

If you need to chain promises in Angular sequentially, you can simply return the promises from one to another: 如果你需要按顺序链接Angular中的promises,你可以简单地将promises从一个返回到另一个:

callFirst()
.then(function(firstResult){
   return callSecond();
})
.then(function(secondResult){
   return callThird();
})
.then(function(thirdResult){
   //Finally do something with promise, or even return this
});

And if you want to return all of this as an API: 如果您想将所有这些作为API返回:

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

Make all methods atomar, by adding their own promises. 通过添加自己的承诺,使所有方法atomar。 In your code, the first resolve will complete the whole request. 在您的代码中,第一个resolve将完成整个请求。

If the methods have their own promise, you can chain them with ease. 如果方法有自己的承诺,你可以轻松地链接它们。

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

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM