Angular:返回$ q.defer()。promise而不是$ http promise

时间:2022-10-07 08:11:27

Watching a lot of Egghead.io videos, I noticed that a common pattern is to return a custom promise and resolve it in the callbacks.

观看了很多Egghead.io视频,我注意到一个常见的模式是返回自定义承诺并在回调中解决它。

.factory('myFact', function($q, $http) {
    return {
        getData: function() {
            var deferred = $q.defer();
            $http.get('/path/to/api')
                .success(function(data) {
                    deferred.resolve(data);
                });
            return deferred.promise;
        }
    };
});

I would normally write this as:

我通常会这样写:

.factory('myFact', function($http) {
    return {
        getData: function() {
            return $http.get('/path/to/api')
                .then(function(res) {
                    return res.data;
                });
        }
    };
});

Is there any advantage to returning a $q.defer() promise rather than an $http promise? The approaches look identical to me.

返回$ q.defer()承诺而不是$ http承诺有什么好处吗?这些方法与我相同。

1 个解决方案

#1


3  

No, no advantages, it's the same, In your first code snipped you created a $q.defer() instance then you invoked its resolve() method to create a resolved promise.

不,没有优势,它是一样的,在你的第一个代码剪切中你创建了一个$ q.defer()实例然后你调用了它的resolve()方法来创建一个已解决的promise。

That is the process you will need to know and pass throw in angularJs when working with asynchronously functions and future objects that will have different values or new data at some future moment which you will need to know when it happens because interested parties in your app may need to get access to the result of the deferred task when it completes.

这是您在处理异步函数和未来对象时需要知道并传递throwJs的过程,这些对象具有不同的值或新数据,您将需要知道它何时发生,因为您的应用程序中的相关方可能会需要在完成时访问延迟任务的结果。

Now when working with $http you don't have to do any of that because it will already return a resolved promise that you can directly invoke it's then() method unless you have a different way to do things and you need to implement a different approach.

现在使用$ http时,您不必执行任何操作,因为它已经返回一个已解析的promise,您可以直接调用它的then()方法,除非您有不同的方法来执行操作并且需要实现不同的做法。

But not all angularJs services are going to do the work for you, get a look to $resource for example, which wraps $http for use in RESTful web API scenarios. $resource will not return a resolved promise, a promise yes, you are getting one but you'll need to do the last step of resolving it (check this stack question or this and maybe this article about Amber Kaplan's own experience working with Rest).

但并非所有angularJs服务都能为您完成工作,例如,查看$ resource,它包含$ http以用于RESTful Web API场景。 $ resource不会返回已解决的承诺,承诺是的,你得到一个,但你需要做最后一步解决它(查看这个堆栈问题或者这篇文章,也许这篇文章是关于Amber Kaplan自己使用Rest的经验) 。

So the way how you are doing it is good, that is how I'm doing it too when working with $http but the first snippet code is the one that we will be all searching for when we will need to do things differently with $http or forcing other services to 'work with' or 'work like' AJAX.

所以你如何做到这一点很好,这就是我在使用$ http时也是这样做的,但是第一个代码片段是我们将需要以不同的方式做事时我们将要搜索的代码。 http或强制其他服务“与'合作'或'像'工作'AJAX。

#1


3  

No, no advantages, it's the same, In your first code snipped you created a $q.defer() instance then you invoked its resolve() method to create a resolved promise.

不,没有优势,它是一样的,在你的第一个代码剪切中你创建了一个$ q.defer()实例然后你调用了它的resolve()方法来创建一个已解决的promise。

That is the process you will need to know and pass throw in angularJs when working with asynchronously functions and future objects that will have different values or new data at some future moment which you will need to know when it happens because interested parties in your app may need to get access to the result of the deferred task when it completes.

这是您在处理异步函数和未来对象时需要知道并传递throwJs的过程,这些对象具有不同的值或新数据,您将需要知道它何时发生,因为您的应用程序中的相关方可能会需要在完成时访问延迟任务的结果。

Now when working with $http you don't have to do any of that because it will already return a resolved promise that you can directly invoke it's then() method unless you have a different way to do things and you need to implement a different approach.

现在使用$ http时,您不必执行任何操作,因为它已经返回一个已解析的promise,您可以直接调用它的then()方法,除非您有不同的方法来执行操作并且需要实现不同的做法。

But not all angularJs services are going to do the work for you, get a look to $resource for example, which wraps $http for use in RESTful web API scenarios. $resource will not return a resolved promise, a promise yes, you are getting one but you'll need to do the last step of resolving it (check this stack question or this and maybe this article about Amber Kaplan's own experience working with Rest).

但并非所有angularJs服务都能为您完成工作,例如,查看$ resource,它包含$ http以用于RESTful Web API场景。 $ resource不会返回已解决的承诺,承诺是的,你得到一个,但你需要做最后一步解决它(查看这个堆栈问题或者这篇文章,也许这篇文章是关于Amber Kaplan自己使用Rest的经验) 。

So the way how you are doing it is good, that is how I'm doing it too when working with $http but the first snippet code is the one that we will be all searching for when we will need to do things differently with $http or forcing other services to 'work with' or 'work like' AJAX.

所以你如何做到这一点很好,这就是我在使用$ http时也是这样做的,但是第一个代码片段是我们将需要以不同的方式做事时我们将要搜索的代码。 http或强制其他服务“与'合作'或'像'工作'AJAX。