【发布时间】:2013-07-12 14:38:13
【问题描述】:
我有一个快速应用程序。我想为要运行的一组函数提供并行流。我正在考虑使用async 模块来这样做。
我想知道有没有比这个更好的模块?
其次我想知道这些函数是否必须是异步的?假设我有这样的代码
var sum = function(x, y){
return (x + y)
}
async.parallel([
function(callback){
setTimeout(function(){
result = sum (x, y); //just an example for a synchronous function
callback(null, result);
}, 100);
},
function(callback){
result = sum (x, y); //just an example for a synchronous function
callback(null, result);
}
],
// optional callback
function(err, results){
console.log(result);
// the results array will equal ['one','two'] even though
// the second function had a shorter timeout.
});
所以你可以在里面有一些同步的函数。那么这两者还会并行运行吗?
我还听说在 node.js 中只有 I/O 任务可以并行运行,因为 node.js 是单线程的。这是真的吗?因此,如果我没有异步模块中的 I/O 任务,它们也不会并行运行,而只是出现?
请帮忙。
【问题讨论】:
标签: node.js asynchronous express node-async