Using AngularJS, I am attempting to execute multiple http POST requests and create an object of successfully finished requests. Here is a sample code snippet:
var params = [1, 2, 3],
url,
i,
done = {};
for (i in params) {
url = '/dir/'+ params[i];
$http.post(url, {"some_request": "not important"}).
success(function(response) {
done[params[i]] = 'successful';
});
}
The desired outcome is an object containing all successful requests like this:
done = {1: 'successful', 2: 'successful', 3: 'successful'};
However, due to the asynchronous nature of http requests, only the last successful request is captured:
done = {3: 'successful'};
Since the loop finishes before the responses are returned, how can the loop index be passed into the responses? Your help is appreciated.