I have a piece of source code like this
var projectPromises = $http.get('http://myapi.com/api/v3/projects?private_token=abcde123456');
$q.all([projectPromises]).then(function(data) {
console.log(data);
return data[0];
}).then(function(projects) {
var data = projects.data;
var promises = [];
var file = [];
for(var i = 0; i < data.length; i++){
var url = 'http://myapi.com/api/v3/projects/' + data[i].id + "/owner";
promises.push($http.get(url));
}
console.log(promises);
$q.all(promises).then(function(user) {
console.log(user);
}, function(error) {
console.log("error here");
console.log(error);
});
Let me explain my source code.
First, I have the first API which will return a list of projects and I assign to projectPromises. After I get the list of projects , each project will contain a project ID . I will loop over the projects list and fire the corresponding http request to get the owner of a project.
After that , I use Angular q module to defer the list of promises and log the list into the console
console.log(user);
It does not log anything here . I try to print the error and I know the reason is that not all projects contain the users list. If not , it will return 404 , and 200 for vice versa. So the promises list will contain both 200 and 404 object return from the API , so I guest that when use q to defer the promises , it throw the error if the object is 404. But I don't know how to fix this.
My final purpose is to get the owner for each project and they will be populated into an array.