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

I am using an asynchronous module to do a parallel task. I basically have two different files, dashboard.js and 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");
   });

I expected func1 and func2 to execute in parallel, but it throws below errors

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)

Why can't I use dashboard.func1, dashboard.func2 , even the dashboard.func1 function is a function?

+4
source share
1 answer

For the async property, I would use a callback function. This feature also benefits non-blocking calls.

With your code, you can try

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: async js parrallel

+1

All Articles