我正在尝试向第三方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
是同步还是异步之后,我可能会采用这种方式,但是想知道这种并行请求的方法是否有问题。
我们将不胜感激,非常感谢。