【发布时间】:2015-08-18 12:15:11
【问题描述】:
在接下来的代码中,我想同时处理多个文件,而无需等待对方结束。为此,我先读取文件(数组),然后调用回调处理这个数组实例的一个元素。
我在这个 javascript 代码中发现了一个问题,恰好在异步 for 循环中,该过程作为同步代码而不是异步执行。
var array = ['string1','string2','string3','string4'];
function processArray (arrayString,callback){
//Read file Example.csv thought sync way
try{
var ifs = new InputFileStream('Example.csv','utf8');
table = ifs.read(0);
ifs.close();
}catch(err){
console.log(err.stack);
}
callback(arrayString, table);
}
//Async for
for (var i=0; i<array.length; i++) {
processArray(array[i], function(arrayString, table){
//Here process the file values thought async way
console.log('processed_'+i);
});
}
【问题讨论】:
-
是什么让你认为这个结构应该是异步的?
-
我猜这个例子是实现for循环异步的正确方法。你知道其他方法吗?
-
我暗示的是这不是异步的。是什么让你认为它是?
-
你能告诉我这个for循环的异步方式吗?
-
什么是
InputFileStream- 我知道的不是标准的javascript
标签: javascript for-loop asynchronous