捕捉两次Angular promise success响应



似乎控制台的输出将永远是:

This is the first response
This is the second response

,因为内部方法的成功函数将首先被调用。这是一个正确的假设吗?订单有保证吗?

app.controller("ctrl", function($scope,$http) {
    var getUrl = function () {
          var config = {
              method: 'GET',
              url: 'some.txt'
          };

          return $http(config)
              .success(function (response, status, headers, config) {
                   console.log('This is the first response');
              })
              .error(function (data, status, headers, config) {
              });
     };   

    var init = function () {
        var promise = getUrl();
        promise.then(
           function() { 
              console.log('This is the second response');
           });
    };
    init();
 });

是的,因为$http本身返回一个承诺,你上面所做的是承诺链接。

使用$http.then(success,error).then(success,error);

最新更新