Skip to content

Instantly share code, notes, and snippets.

@rsvalerio
Created December 11, 2012 01:33
Show Gist options
  • Save rsvalerio/4255005 to your computer and use it in GitHub Desktop.
Save rsvalerio/4255005 to your computer and use it in GitHub Desktop.
MultiPromise Angular Service
'use strict';
/**
* CTRL
* $scope.requisitar = function () {
* MultiPromise.async([
* 'http://localhost:9090/?sleep=10000',
* 'http://localhost:9090/?sleep=1000',
* 'http://localhost:9090/?sleep=5000'
* ])
* .then(function (response) {
* console.log('result');
* console.log(response);
* });
* };
*/
myApp.factory('MultiPromise', ['$http','$q',function($http, $q) {
function prom(url){
return $http.get(url).then(function(response) {
return response.data;
});
}
return {
async: function(urls) {
var proms = [];
for(var i =0;i<urls.length;i++){
proms.push(prom(urls[i]));
}
var returnPromise = $q.all(proms);
return returnPromise;
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment