I would like to implement promises in my AngularJS application like you can see in this example: https://stackoverflow.com/a/12360882/371273
My application will take multiple arrays of data making multiple database hits on my server, but I have configured my server to return all of that data as one response in the idea of minimizing requests to keep my application as efficient as possible (the data is essentially useless unless all of the data gets to the client). So instead of doing this...
MyCtrl.resolve = {
projects : function($q, $http) {
return $http.get('/api/projects'});
},
clients : function($q, $http) {
return $http.get('/api/clients'});
}
};
I would like to be able to make a single $http
GET request to a URL like /api/allData
, and then have a promise for projects
that will be set equal to allData.projects
and a promise for clients
that will be set equal to allData.clients
, etc. (where allData
is the data that came back with my single request). I'm still very new to promises, can someone give me an example of how I would set up these promises inside MyCtrl.resolve
?
Well, there is not much of a point in using two promises that you know represent the same promise. But you could indeed create a service that receive handles the one promise and return two. Something like this:
But as I said, there is not much of a point in this. Maybe a better solution, considering your service will return an array, would be return an object with empty arrays and fill them when the $http promise gets completed.
I believe that's what you are looking for:
The you can get your data from the final
aggregatedData
array (e.g.:aggregatedData[0], aggregatedData[1],...)You can set up these promises inside MyCtrl.resolve in a single call: