I put an answer with an idea that no one has mentioned yet, and that I think is the best option. Using Promise.all
.
When you describe you need "before touching, ensure that all files have been read in memory." That’s exactly what the Promise.all
ago.
Passing an array of Promises to that Promise. she hopes that all are solved and returns a Promise
which receives as argument of the success method 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 in the same, calling the Promise.resolve
immediately. That is why Promise.all
accepts a mixed array, with Promises or not.
A simple example would be like this: https://jsfiddle.net/4s5L4s7c/, what you need in your case would be 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!');
});
Can you explain in what context you want to use this feature? Without knowing what you want to do we’ll be guessing a solution that may not be useful.
– Sergio
You could specify that this is using an http script: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise#Creating_a_promise_2
– Guilherme Lautert
I have a videoplayer where the configuration of what will play is described in several files. I need, before touching, to ensure that all the files have been read in memory. To get the file, I have to access a WS that returns me this.
– Paulo Gustavo