I put a response with an idea that nobody has mentioned yet, and that I think is the best option. Using Promise.all
.
When you describe what you need "before touching, make sure all files have been read in memory." this is exactly what Promise.all
does.
Passing an array of Promises to this Promise.all expects all to be resolved and returns a% of the method of success an array with the values of the past Promises. If you pass static values (Strings, Objects, or other variables that are not asynchronous) it uses it, calling Promise
immediately. This is why Promise.resolve
accepts a mixed array, with Promises or not.
A simple example would look like this: link , what you need in your case would look something like this:
var urls = ['/pasta/a', '/pasta/b' /* etc... */ ];
function get(url, i) {
return $http.get(url); <-- isto será o que queres
}
Promise.all(urls.map(get)).then(function(res) {
console.log(res); // aqui "res" é uma array com a resposta de cada "$http.get", na mesma ordem da array "urls".
}, function() {
console.log('Algo falhou!');
});