如果服务暴露自己的不同步? [英] Should services expose their asynchronicity?
问题描述
我正在写一个服务,将异步检索数据($ HTTP或$资源)。我可以隐藏的事实,它是通过返回一个数组,最初是空的异步的,但最终会填充:
I'm writing a service that will retrieve data asynchronously ($http or $resource). I can hide the fact that it is asynchronous by returning an array that will initially be empty, but that will eventually get populated:
.factory('NewsfeedService1', ['$http', function($http) {
var posts = [];
var server_queried = false;
return {
posts: function() {
if(!server_queried) {
$http.get('json1.txt').success(
function(data) {
server_queried = true;
angular.copy(data, posts);
});
}
return posts;
}
};
}])
.controller('Ctrl1', ['$scope','NewsfeedService1',
function($scope, NewsfeedService1) {
$scope.posts = NewsfeedService1.posts();
}])
或者,我可以通过返回的承诺揭露异步性:
Or I can expose the asynchronicity by returning a promise:
.factory('NewsfeedService2', ['$http', function($http) {
var posts = [];
var server_queried = false;
var promise;
return {
posts_async: function() {
if(!promise || !server_queried) {
promise = $http.get('json2.txt').then(
function(response) {
server_queried = true;
posts = response.data;
return posts;
});
}
return promise;
}
};
}])
.controller('Ctrl2', ['$scope','NewsfeedService2',
function($scope, NewsfeedService2) {
NewsfeedService2.posts_async().then(
function(posts) {
$scope.posts = posts;
});
// or take advantage of the fact that $q promises are
// recognized by Angular's templating engine:
// (note that Peter and Pawel's AngularJS book recommends against this, p. 100)
$scope.posts2 = NewsfeedService2.posts_async();
}]);
( Plunker - 如果有人想玩弄上述两种实现)
(Plunker - if someone wants to play around with the above two implementations.)
露出的异步性会增加一个错误处理程序的则()
的方法,我可以处理控制器错误的一个潜在优势。不过,我很可能会再追和$ HTTP错误在处理应用程序范围的拦截。
One potential advantage of exposing the asychronicity would be that I can deal with errors in the controller by adding an error handler to the then()
method. However, I'll likely be catching and dealing with $http errors in an application-wide interceptor.
所以,当要服务的异步性暴露?
So, when should a service's asynchronicity be exposed?
推荐答案
我的猜测是,你会发现人们对这个栅栏的两侧。的个人的,我觉得你应该总是暴露的库或函数的异步性(或更正确:我觉得你应该的从不掩饰的库或函数的异步性) 。主要的原因是透明度;例如,将这项工作?
My guess is that you'll find people on both sides of this fence. Personally, I feel that you should always expose the asynchronicity of a library or function (or more correctly: I feel that you should never hide the asynchronicity of a library or function). The main reason is transparency; for example, will this work?
app.controller('MyController', function(NewsfeedService) {
$scope.posts = NewsfeedService.posts();
doSomethingWithPosts($scope.posts); // <-- will this work?
});
如果你使用第一种方法(例如: $资源
),它不会,即使 $ scope.posts
在技术上是一个数组。如果 doSomethingWithPosts
都有自己的异步操作,你可以用一个竞争条件结束。相反,你无论如何都要使用异步code:
If you're using the first method (e.g. $resource
), it won't, even though $scope.posts
is technically an array. If doSomethingWithPosts
has its own asynchronous operations, you could end up with a race condition. Instead, you have to use asynchronous code anyway:
app.controller('MyController', function(NewsfeedService) {
$scope.posts = NewsfeedService.posts(function() {
doSomethingWithPosts($scope.posts);
});
});
(当然,你可以回调接受帖子
作为参数,但我仍然认为这是混乱和不规范。)
(Of course, you can make the callback accept the posts
as an argument, but I still think it's confusing and non-standard.)
幸运的是,我们有承诺,承诺的目的就是要的再present 的操作的未来价值。此外,由于与承诺角的 $ Q
库创建可以绑定到的意见,有没有错:
Luckily, we have promises, and the very purpose of a promise is to represent the future value of an operation. Furthermore, since promises created with Angular's $q
libraries can be bound to views, there's nothing wrong with this:
app.controller('MyController', function(NewsfeedService) {
$scope.posts = NewsfeedService.posts();
// $scope.posts is a promise, but when it resolves
// the AngularJS view will work as intended.
});
[更新:你不能再直接绑定承诺的观点;你必须等待承诺解决,手动分配scope属性]
[Update: you can no longer bind promises directly to the view; you must wait for the promise to be resolved and assign a scope property manually.]
顺便说一句, Restangular ,一个流行的替代 $资源
,使用承诺,AngularJS自己的 $资源
将在1.2支持他们(它们可能已经支持他们最新的1.1.x版本的)。
As an aside, Restangular, a popular alternative to $resource
, uses promises, and AngularJS' own $resource
will be supporting them in 1.2 (they may already support them in the latest 1.1.x's).
这篇关于如果服务暴露自己的不同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!