Chain promises with AngularJS

后端 未结 2 1814
生来不讨喜
生来不讨喜 2020-12-05 17:49

I have a service called paymentStrategy that get injected in my controller.

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


        
2条回答
  •  清歌不尽
    2020-12-05 18:29

    If you need to chain promises in Angular sequentially, you can simply return the promises from one to another:

    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:

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

提交回复
热议问题