我写了这个函数,它应该运行这个for循环,然后在最后回调,但它在循环完成之前一直运行回调。我读过的唯一解决方案是使用回调来解决这个问题,但它似乎没有太大区别。Nodejs for循环回调问题
function run(callback){
for(var i = 0; i < urls.length; i++){
request(url, function(error, resp, body){
//uses cheerio to iterate through some elements on the page
//then saves to an object for a future response
});
callback();
}
}
什么是“做一些东西”异步? –
是的,现在有一个在循环中运行的请求来更新代码。 – txnnxr