异步for循环:如何让for循环等待函数完成
Asynchronous for loop: how do I make the for loop wait for function to finish?
我绞尽脑汁想弄清楚如何对这个/地方回调进行排序以获得我需要的东西。
我有一个检查文件是否存在的循环。我希望它在完成时回调,但是for
循环和回调在"fs"之前完成。打开"结束…典型的异步问题
我正在使用节点v0.10.29, express v3.14.0,并且正在使用"async"库,但是再次,只是无法找出我需要的逻辑…
我有:
输入function checkForAllFiles(callback)
{
var requiredFiles = [];
requiredFiles[requiredFiles.length] = "../Client/database/one.js";
requiredFiles[requiredFiles.length] = "../Client/database/two.dat";
requiredFiles[requiredFiles.length] = "../Client/database/three.xml";
requiredFiles[requiredFiles.length] = "../Client/database/four.dat";
requiredFiles[requiredFiles.length] = "../Client/database/five.dat";
requiredFiles[requiredFiles.length] = "../Client/database/six.xml";
var missingFiles = [];
for(var r=0; r<requiredFiles.length; r++)
{
fs.open(requiredFiles[r], 'r', function(err, fd){
if(err)
{
missingFiles[missingFiles.length] = err.path;
console.log("found missing file = ", err.path);
}
});
console.log("r = ", r);
}
console.log("sending callback: ", missingFiles);
callback(missingFiles);
}
<标题> 输出0
1
2
3
4
5
sending callback: []
found missing file: ../Client/database/three.xml
<标题>期望输出值0
1
found missing file: ../Client/database/three.xml
2
3
4
5
sending callback: ["../Client/database/three.xml"]
标题>标题>我会在async模块中使用reject
方法(我看到您已经找到了)。它将在回调函数中返回一个数组,其中包含与指定的检查函数不匹配的任何元素。对于检查功能,我建议只使用fs.exists
,而不是监视fs.open
上的错误。
使用这些函数实际上可以将整个检查减少到一行。像这样:
function checkForAllFiles(callback)
{
var requiredFiles = [];
requiredFiles[requiredFiles.length] = "../Client/database/one.js";
requiredFiles[requiredFiles.length] = "../Client/database/two.dat";
requiredFiles[requiredFiles.length] = "../Client/database/three.xml";
requiredFiles[requiredFiles.length] = "../Client/database/four.dat";
requiredFiles[requiredFiles.length] = "../Client/database/five.dat";
requiredFiles[requiredFiles.length] = "../Client/database/six.xml";
async.reject(requiredFiles, fs.exists, callback);
}
callback
将使用一个数组调用,该数组只包含不存在的文件。
使用异步库和eachSeries方法。例子:
async.eachSeries(array,
function(element, next) {
// do something with element
next();
}
);
它将依次遍历数组并处理每个元素。调用next将转到下一个元素。Series确保它按照数组的顺序执行,否则不能保证遍历数组的顺序。如果你在其中调用了其他异步函数,只需传递下一个函数,并在完成所有需要的函数后调用它,下一个数组元素将被处理。
也许像这样:
var missingFiles = []
async.eachSeries(requiredFiles, function(file, nextFile){
fs.open(file, 'r', function(err, fd){
if(err)
{
missingFiles[missingFiles.length] = err.path;
console.log("found missing file = ", err.path);
}
nextFile();
});
console.log("r = ", file);
});
console.log("sending callback: ", missingFiles);
callback(missingFiles);
相关文章:
- 为什么JavaScript在for循环为3时向所有4发出警报
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 我的javascript for循环不起作用
- For循环冻结Javascript
- 如何在for循环中添加事件侦听器
- 双“for”循环(循环)
- javascript for循环不起作用
- for循环中的javascript if语句找不到==
- Javascript在for循环中等待处理请求
- For循环在Jquery中只运行一次
- 如何在for循环中使用计数器
- for循环中的JavaScript闭包
- 为什么我们在ES2015中需要一个新的for循环结构,而我们已经有了for、forEach
- For循环在调用时未运行
- 如何使用for循环添加所有按钮'单击事件
- 如何更改在for循环中生成的圆的位置
- 为什么这个For循环会使浏览器实验室崩溃
- 为什么我使用javascript获得了一个无限的for循环
- 在for循环中使用多维数组设置google.maps.Marker图标
- 如何在angularJS中运行for循环而不使用html标记