If I have an array of urls:
var urls = ['1.txt', '2.txt', '3.txt']; // these text files contain one, two, three, respectively.
And I want to build an object that looks like this:
var text = ['one', 'two', 'three'];
I’ve been trying to learn to do this with fetch
, which of course returns Promise
s.
Some things I’ve tried that don’t work:
var promises = urls.map(url => fetch(url));
var texts = [];
Promise.all(promises)
.then(results => {
results.forEach(result => result.text()).then(t => texts.push(t))
})
This doesn’t look right, and in any case it doesn’t work — I don’t end up with an array ['one', 'two', 'three'].
Is using Promise.all
the right approach here?