4

I am using the async module to execute parallel tasks. Basically, I 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 was expecting func1 and func2 to execute in parallel, but it is throwing the below error.

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 I not use dashboard.func1, dashboard.func2 even if dashboard.func1 is a function?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80
  • These snippets don't seem likely to be exactly the code you're executing. You should be getting rather different errors. 1) [Directional quotations](https://en.wikipedia.org/wiki/Quotation_mark#Unicode_code_point_table) aren't valid delimiters for string literals. You should be seeing `SyntaxError`s for those. 2) To `require()` [another file in your project](https://nodejs.org/dist/latest-v5.x/docs/api/modules.html#modules_file_modules), the path should be relative to the current file. `require('dashboard.js')` expects to find `node_modules/dashboard.js`. – Jonathan Lonowski Apr 17 '16 at 17:37
  • try: adding bracket. For example. dashboard.func1() Hope this work – Michael Seltene Apr 17 '16 at 17:40
  • Good observation , I have posted sudo code not working code , assume require('./dashboard.js') @JonathanLonowski – Sumeet Kumar Yadav Apr 17 '16 at 17:41
  • dashboard.func1() does not work , it gives same error @MichaelSeltene – Sumeet Kumar Yadav Apr 17 '16 at 17:42
  • putting method inside function works like function(){dashboard.func1();} – Sumeet Kumar Yadav Apr 17 '16 at 17:43
  • 1
    @Sumeet If wrapping the call in another function works, does your application have a [cycle](https://nodejs.org/dist/latest-v5.x/docs/api/modules.html#modules_cycles) (circular reference)? If it does, to handle these, Node.js will only load one module up to the `require()` that starts the cycle. It will have to wait to finish loading it until after the other module(s) involved have loaded completely. – Jonathan Lonowski Apr 17 '16 at 17:45
  • @Sumeet Another possibility is that `module.exports` in `dashboard.js` is being assigned within a callback for an asynchronous operation. In that case, despite spanning multiple modules, it would be the same problem as [Why is my variable unaltered after I modify it inside of a function?](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). – Jonathan Lonowski Apr 17 '16 at 18:00

1 Answers1

1

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

With your code, you could try

File 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);
   }
}

File 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);
     }
   });
});

There is also a question similar to yours: Best way to execute parallel processing in Node.js

In addition, a specific answer for the error is in: TypeError: task is not a function in async js parrallel

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Seltene
  • 543
  • 1
  • 5
  • 17