通过添加自己的承诺使所有方法变得原子化。在您的代码中,第一个
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); } };});



