declaring a promise in angularJS with named success/error callbacks

前端 未结 3 1467
执念已碎
执念已碎 2021-02-02 13:56

I am trying to do something very similar to the $http service. From my understanding $http return a promise object.

When using it the syntax is :

$http(.         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 14:46

    The nice thing with open-source is that you can read the source. Here's how the $http service does it:

      promise.success = function(fn) {
        promise.then(function(response) {
          fn(response.data, response.status, response.headers, config);
        });
        return promise;
      };
    
      promise.error = function(fn) {
        promise.then(null, function(response) {
          fn(response.data, response.status, response.headers, config);
        });
        return promise;
      };
    

提交回复
热议问题