Node.js async parallel "TypeError: task is not a function"

Node.js async parallel "TypeError: task is not a function"

我正在使用异步模块来执行并行任务。基本上,我有两个不同的文件,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 是一个函数?

对于异步 属性,我会使用回调功能。此功能也有利于非阻塞调用。

用你的代码,你可以试试

文件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);
     }
   });
});

还有一个问题和你的类似:Best way to execute parallel processing in Node.js

此外,错误的具体答案在: