Se ho una serie di URL:Come posso recuperare un array di URL con Promise.all?
var urls = ['1.txt', '2.txt', '3.txt']; // these text files contain "one", "two", "three", respectively.
E voglio costruire un oggetto che assomiglia a questo:
var text = ['one', 'two', 'three'];
Ho cercato di imparare a fare questo con fetch
, che naturalmente restituisce Promise
s.
Alcune cose che ho provato che non lo fanno lavoro:
var promises = urls.map(url => fetch(url));
var texts = [];
Promise.all(promises)
.then(results => {
results.forEach(result => result.text()).then(t => texts.push(t))
})
Questo non guardare a destra, e in ogni caso non funziona - io non finire con un array ['one', 'two', 'three'].
Sta usando Promise.all
l'approccio giusto qui?
Che assomiglia a un errore di bracketing. Volevi davvero chiamare '.then' sul valore di ritorno di' .forEach (...) ', o piuttosto su' ... .text() '? – Bergi
Dove guardate/registrate 'testi' e osservate che è ancora vuoto? – Bergi