我有两个包含URL的嵌套数组,例如:
[['http://ex.com/1','http://ex.com/2'],['http://ex.com/3']]
在解析第一个数组后,如何同时从第一个数组中获取URL和如何从第二个数组中获取URL?
我有两个包含URL的嵌套数组,例如:
[['http://ex.com/1','http://ex.com/2'],['http://ex.com/3']]
在解析第一个数组后,如何同时从第一个数组中获取URL和如何从第二个数组中获取URL?
您可以使用Promise.all()
一次获取几个网址。
async function getResponses (arr) {
const res = []
for (const urls of arr) {
// Fetch all urls at once
const responses = await Promise.all(urls.map(url => fetch(url)))
res.push(await Promise.all(responses.map(response => response.text())))
}
return res
}
如果您的回复使用JSON,则可以使用
responses.map(response => response.json())