admin管理员组文章数量:1321428
EDIT
The first answer is the elegant one, but, as stated a few times in this question and another questions on stackoverflow, the problem is that the service and the controller run their thing before the data actually arrives.
(Last comment on the first answer:)
Yes, the problem is that the API calls finish AFTER the service runs and returns everything to the controller, see here screencast/t/uRKMZ1IgGpb7 ... That's my BASE question, how could I wait on all the parts for the data to arrive?
It's like I'm saying it on repeat, how do we make a service that populates the array after the successful data retrieval, and the controller getting data after all this happens, because as you can see in my screenshot, things run in a different order.
I have this code:
var deferred = $q.defer();
$http.get('../wordpress/api/core/get_category_posts/?category_id=14 ').success(function(data) {
//we're emptying the array on every call
theData = [];
catName = data.category.slug;
theData = data;
theData.name = catName;
aggregatedData.push(theData);
});
$http.get('../wordpress/api/core/get_category_posts/?category_id=15 ').success(function(data) {
theData = [];
catName = data.category.slug;
theData = data;
theData.name = catName;
aggregatedData.push(theData);
});
$http.get('../wordpress/api/core/get_category_posts/?category_id=16 ').success(function(data) {
theData = [];
catName = data.category.slug;
theData = data;
theData.name = catName;
aggregatedData.push(theData);
});
$http.get('../wordpress/api/core/get_category_posts/?category_id=17 ').success(function(data) {
theData = [];
catName = data.category.slug;
theData = data;
theData.name = catName;
aggregatedData.push(theData);
});
//deferred.resolve(aggregatedData);
$timeout(function() {
deferred.resolve(aggregatedData);
}, 1000);
/*//deferred.reject('There is a connection problem.');
if (myservice._initialized) {
$rootScope.$broadcast('postsList', deferred.promise);
}*/
//myservice._initialized = true;
myservice = deferred.promise;
return deferred.promise;
For the life of me I can't understand why do I have to put a timeout when passing the resulting array to defer ?
Shouldn't the principle be like, defer waits for the information to come and then returns the promise? What is the point of that 1 second there? From what I understand defer should be able to wait as long as needed for the API to return the result and the return the promised data.
I'm really confused, I've banged my head against the walls for the last two hours because I was not receiving any data in my controller, only when I put that timeout there.
EDIT
The first answer is the elegant one, but, as stated a few times in this question and another questions on stackoverflow, the problem is that the service and the controller run their thing before the data actually arrives.
(Last comment on the first answer:)
Yes, the problem is that the API calls finish AFTER the service runs and returns everything to the controller, see here screencast.com/t/uRKMZ1IgGpb7 ... That's my BASE question, how could I wait on all the parts for the data to arrive?
It's like I'm saying it on repeat, how do we make a service that populates the array after the successful data retrieval, and the controller getting data after all this happens, because as you can see in my screenshot, things run in a different order.
I have this code:
var deferred = $q.defer();
$http.get('../wordpress/api/core/get_category_posts/?category_id=14 ').success(function(data) {
//we're emptying the array on every call
theData = [];
catName = data.category.slug;
theData = data;
theData.name = catName;
aggregatedData.push(theData);
});
$http.get('../wordpress/api/core/get_category_posts/?category_id=15 ').success(function(data) {
theData = [];
catName = data.category.slug;
theData = data;
theData.name = catName;
aggregatedData.push(theData);
});
$http.get('../wordpress/api/core/get_category_posts/?category_id=16 ').success(function(data) {
theData = [];
catName = data.category.slug;
theData = data;
theData.name = catName;
aggregatedData.push(theData);
});
$http.get('../wordpress/api/core/get_category_posts/?category_id=17 ').success(function(data) {
theData = [];
catName = data.category.slug;
theData = data;
theData.name = catName;
aggregatedData.push(theData);
});
//deferred.resolve(aggregatedData);
$timeout(function() {
deferred.resolve(aggregatedData);
}, 1000);
/*//deferred.reject('There is a connection problem.');
if (myservice._initialized) {
$rootScope.$broadcast('postsList', deferred.promise);
}*/
//myservice._initialized = true;
myservice = deferred.promise;
return deferred.promise;
For the life of me I can't understand why do I have to put a timeout when passing the resulting array to defer ?
Shouldn't the principle be like, defer waits for the information to come and then returns the promise? What is the point of that 1 second there? From what I understand defer should be able to wait as long as needed for the API to return the result and the return the promised data.
I'm really confused, I've banged my head against the walls for the last two hours because I was not receiving any data in my controller, only when I put that timeout there.
Share Improve this question edited Sep 7, 2013 at 19:03 Arthur Kovacs asked Sep 7, 2013 at 13:38 Arthur KovacsArthur Kovacs 1,7402 gold badges17 silver badges24 bronze badges 5 |2 Answers
Reset to default 60IMHO I think there's a much clever (and elegant) way to do this with $q.all
.
Please take a look at the code below.
I am assuming that you want to return the data at once with all the results aggregated on a big array.
var myApp = angular.module('myApp', []);
myApp.factory('myService', function ($http, $q) {
return {
getAllData: function () {
return $q.all([
$http.get('../wordpress/api/core/get_category_posts/?category_id=14'),
$http.get('../wordpress/api/core/get_category_posts/?category_id=15'),
$http.get('../wordpress/api/core/get_category_posts/?category_id=16'),
$http.get('../wordpress/api/core/get_category_posts/?category_id=17')
]).then(function (results) {
var aggregatedData = [];
angular.forEach(results, function (result) {
aggregatedData = aggregatedData.concat(result.data);
});
return aggregatedData;
});
}
};
});
You can see above that the aggregatedData
is only generated once all the async calls are completed via the $q.all
.
You just need to include the service as dependency into one of your controllers, for example, and call the service like this myService.getAllData()
Let me know if you need a full working example and I can provide one! :)
The $http.get
calls are async, but you aren't waiting until they are all completed before resolving the deferred. Here it works with the timeout simply because your are lucky that the calls have time to complete within 1 second, however this isin't reliable at all.
I will not reiterate a complete solution here, but have a look at my answer for another similar issue.
本文标签: javascriptAngularJS PromisesQdeferStack Overflow
版权声明:本文标题:javascript - AngularJS Promises, $q, defer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737362674a1983780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$http.get
return values into$q.all
? – Steve Klösters Commented Sep 7, 2013 at 14:14