我有两个包含网址的嵌套数组,例如:

[['http://ex.com/1','http://ex.com/2'],['http://ex.com/3']]

在解析第一个数组后,我如何同时从第一个数组中获取URL并从第二个数组中获取URL?

最佳答案

您可以使用Promise.all()一次获取几个URL。

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())

09-09 21:37
查看更多