hibernate/等待,直到进程在asynchronous循环过程中完成

我加载一个JSON数据与请求(长度125664),并从该数据,我做循环与,当然我不能一次全部显示,因为它可能会导致内存泄漏等,如何使用在每一行的rest时间,我尝试了各种方法,从setTimeout等,但没有任何工作

目前我的代码是:

 request({ url: config.url.productSource, json: true }, function(error, response, body) { console.log(body.length); for(let link of body) { console.log(link); product(link); // call puppeteer headless browser (async) stats(link); // call puppeteer headless browser (async) } }); 

看起来你想asyncawait 。 改变你的callbackasyncfunction,并await木偶电话。

 request({ url: config.url.productSource, json: true }, async (error, response, body) => { console.log(body.length); for(let link of body) { console.log(link); await product(link); // call puppeteer headless browser (async) await stats(link); // call puppeteer headless browser (async) } }); 

假设您的产品和统计function已经是asynchronous的。

我无法弄清楚你正在尝试做什么,如果它是并行调用函数产品和统计或确保foreach循环等待,直到两个asynchronous调用完成,所以我无法去准确代码,无论哪种方式,nodejs中都有一个名为async的模块,可以让你玩你想做的事情。 这是一个问题,组织你的algorithm,并构build正确的并行或串行调用。

看看这里: asynchronous