Created
December 11, 2012 01:33
-
-
Save rsvalerio/4255005 to your computer and use it in GitHub Desktop.
MultiPromise Angular Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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