Node.jsasynchronous并行TypeError:任务不是一个函数

我正在使用asynchronous模块来执行并行任务。 基本上我有两个不同的文件,dashboard.js和Run.js。

Dashboard.js

module.exports = { func1 : function(){ console.log(“Funtion one”); }, func2 : function(){ console.log(“Funtion two”); } } 

Run.js

  var dashboard = require('dashboard.js'); var async = require('async'); async.parallel([dashboard.func1, dashboard.func2],function(err){ if(err)throws err; console.log(“ All function executed”); }); 

我期待func1和func2并行执行,但是抛出错误

 TypeError: task is not a function at C:\Users\..\java\realtime-preview\node_modules\async\lib\async.js:718:13 at async.forEachOf.async.eachOf (C:\Users\..\java\realtime-preview\node_modules\async\lib\async.js:233:13) at _parallel (C:\Users\\java\realtime-preview\node_modules\async\lib\async.js:717:9) 

为什么我不能使用dashboard.func1,dashboard.func2甚至dashboard.func1是函数?

对于asynchronous属性,我会使用callbackfunction。 此function也有利于非阻塞呼叫。

用你的代码,你可以试试

Dashboard.js

 module.exports = { func1 : function(callback){ var value = “Function one”; //if value happens to be empty, then undefined is called back callback(undefined|| value); }, func2 : function(callback){ var value = “Function two”; //if value happens to be empty, then undefined is calledback callback(undefined|| value); } } 

Run.js

 var dashboard = require('dashboard.js'); //func1 dashboard.func1(function(callback){ //if callback then do the following if(callback){ console.log(callback); //if no data on callback then do the following }else{ console.error('Error: ' + callback); } }); //func2 dashboard.func2(function(callback){ //if callback then do the following if(callback){ console.log(callback); //if no data on callback then do the following }else{ console.error('Error: ' + callback); } }); }); 

在以下链接上也有类似的问题: 在Node.js中执行并行处理的最佳方法

此外,错误的具体答案是在以下链接: TypeError:任务不是一个函数在asynchronousjs平行