带有地图的并行请求和带有promise.all的for循环,然后

我正在尝试向第三方API运行数百个关键字的并行请求,并且每个请求上都有五种不同类型的请求,它可以正常工作,但是在诺言解决之后,我不得不进一步处理数据,有时会更早一些


const myFunc = async () => {

  const keywords = ['many','different','type','of','keywords']

  // Create promise's array      
  let promises = keywords.map((keyword,index) =>
    new Promise(resolve => setTimeout(() => {
      for (let page = 1; page <= 5; page++) 
        resolve(request(keyword,page))
      },index * 100)
   ))

  // Resolve
  await Promise.all(promises).then(() => { 
    // Here is where I hope to be dealing with the fully resolved data to read and write the file 
  })
}

request函数调用API,然后将结果附加到一个csv文件中,我想做的是将最后一个诺言附加到文件后,我想读取该文件并处理其数据,此时,我遇到了问题,例如csv格式错误。

在不确定fs是同步还是异步之后,我可能会采用这种方式,但是想知道这种并行请求的方法是否有问题。

我们将不胜感激,非常感谢。

yu8023fei 回答:带有地图的并行请求和带有promise.all的for循环,然后

您需要两个 Promise.all-一个在new Promise的循环中,一个在外面等待所有请求完成:

const delay = ms =>  new Promise(resolve => setTimeout(resolve,ms));
const pageCount = 5;
const myFunc = async () => {
  const keywords = ['many','different','type','of','keywords']
  const promises = keywords.map((keyword,index) =>
    delay(index * 100 * pageCount).then(() => Promise.all(Array.from(
      { length: pageCount },(_,i) => delay(100 * (i + 1)).then(() => request(keyword,i + 1))
    )))
  );
  await Promise.all(promises).then(() => { 
    // Here is where I hope to be dealing with the fully resolved data to read and write the file 
  })
}

由于每个呼叫都需要有另一个延迟,因此使用for..of循环可能会更容易:

const delay = ms =>  new Promise(resolve => setTimeout(resolve,'keywords']
  for (const keyword of keywords) {
    for (let page = 1; page <= 5; page++) {
      await delay(100);
      await request(keyword,page);
    }
  }
  // Here is where I hope to be dealing with the fully resolved data to read and write the file
};
本文链接:https://www.f2er.com/2906727.html

大家都在问