我在 nodeJS 的 async.parallel 模块中有查询
I have query in async.parallel module in nodeJS
我正在尝试为我的项目使用 async
节点模块,我非常需要它。当我尝试按照代码进行操作时,它不起作用。请任何人帮助我解决问题。
async.parallel({
test : function(callback) {
for(var i=0; i<1000000000; i++){
if(i == 999999999){
console.log("test");
callback(null, "test");
}
}
},
test1 : function(callback) {
console.log("test1")
callback(null, "test1");
}
}, function(err, results) {
// optional callback
console.log('results', results)
});
这些代码将如何执行?我试过它的 print { test: 'test', test1: 'test1' }
但从逻辑上讲,如果并行进程执行而不需要像 { test1: 'test1', test: 'test' }
.
这样的输出
如果我使用 timeout
而不是 for loop
,那么它工作正常。
这来自异步 doc
Note: parallel is about kicking-off I/O tasks in parallel, not about parallel execution of code. If your tasks do not use any timers or perform any I/O, they will actually be executed in series. Any synchronous setup sections for each task will happen one after the other. JavaScript remains single-threaded.
所以,你的 For 循环 是以同步方式执行任务
我正在尝试为我的项目使用 async
节点模块,我非常需要它。当我尝试按照代码进行操作时,它不起作用。请任何人帮助我解决问题。
async.parallel({
test : function(callback) {
for(var i=0; i<1000000000; i++){
if(i == 999999999){
console.log("test");
callback(null, "test");
}
}
},
test1 : function(callback) {
console.log("test1")
callback(null, "test1");
}
}, function(err, results) {
// optional callback
console.log('results', results)
});
这些代码将如何执行?我试过它的 print { test: 'test', test1: 'test1' }
但从逻辑上讲,如果并行进程执行而不需要像 { test1: 'test1', test: 'test' }
.
如果我使用 timeout
而不是 for loop
,那么它工作正常。
这来自异步 doc
Note: parallel is about kicking-off I/O tasks in parallel, not about parallel execution of code. If your tasks do not use any timers or perform any I/O, they will actually be executed in series. Any synchronous setup sections for each task will happen one after the other. JavaScript remains single-threaded.
所以,你的 For 循环 是以同步方式执行任务