简体   繁体   English

$ http获取返回承诺角

[英]$http get return a promise angular

I have this code in a service, it works for me. 我在服务中有此代码,它对我有用。 As far as I know, $http.get() returns a promise, promise executes asynchronously, so why do I need to use deffered.resolve(res.data) to return data in my service. 据我所知, $http.get()返回一个promise,promise异步执行,所以为什么我需要使用deffered.resolve(res.data)返回服务中的数据。 Thanks a lot. 非常感谢。

 data.posts = function(page, perPage, format, orderBy) {
        var deffered = $q.defer();
        $http.get(hostName, {
            params: {
                'page': page,
                'per_page': perPage,
                'filter[post_format]=': format,
                'filter[orderby]=': orderBy,
                'order': 'desc'
            }
        }).then(function(res) {
            deffered.resolve(res.data);
        })
        return deffered.promise;
    }

If truly in a service, deferred not needed. 如果确实在服务中,则不需要推迟。 The method in the service returns the promise from the above $http request. 服务中的方法从上述$ http请求中返回promise。

function exampleService($http) {
    var data = this;
    data.post = function(page, perPage, format, orderBy) {
      return $http.get(hostName, {
          params: {
            'page': page,
            'per_page': perPage,
            'filter[post_format]=': format,
            'filter[orderby]=': orderBy,
            'order': 'desc'
          }
        }).then(function(res) {
          //do stuff with success
        })
        .catch(function(err) {
          //do stuff with error
        })
        .finally(function() {
          //optionally use this as well maybe if you had loading type overlay/icon
        });
    };
  }
  //preferred method as it makes methods available before the sevice has been returned  

function exampleService($http) {
  function post(page, perPage, format, orderBy) {
      return $http.get(hostName, {
          params: {
            'page': page,
            'per_page': perPage,
            'filter[post_format]=': format,
            'filter[orderby]=': orderBy,
            'order': 'desc'
          }
        }).then(function(res) {
          //do stuff with success
        })
        .catch(function(err) {
          //do stuff with error
        })
        .finally(function() {
          //optionally use this as well maybe if you had loading type overlay/icon
        });
    }
    //revealing module pattern 
  return {
    post: post,

  };
}

Why use it? 为什么要使用它? Because the author didn't know better. 因为作者并不了解。 There are however many reasons not to use it . 但是,有很多原因不使用它

The code should read 该代码应阅读

data.posts = function(page, perPage, format, orderBy) {
    return $http.get(hostName, {
        params: {
            'page': page,
            'per_page': perPage,
            'filter[post_format]=': format,
            'filter[orderby]=': orderBy,
            'order': 'desc'
        }
    }).then(function(res) {
        return res.data;
    });
};

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

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