我有多个 URL 可以从中调用数据,我想在它们全部响应并收到数据后调用一个函数。
var promises = urls.map(url => fetch(url));
Promise.all(promises).then(response => {
for (var i = 0; i < response.length; i++) {
response[i].json().then(data => { dataReceived.push(data.rows)})
}
}).then(dataReceived=> {
doThisFucntion(withAllTheData);
});
我确定我只需要添加另一个 promise.all() 但我不知道在哪里做。
谢谢
最佳答案
可能是这样的:
var promises = urls.map(url => fetch(url));
Promise.all(promises)
.then(response => Promise.all(response.map(resp=>resp.json())))
.then(data=>data.map(element=>element.rows))
.then(dataReceived=> {
doThisFucntion(withAllTheData);
});
关于javascript - 等待所有响应,然后调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48940052/