5

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?

7
  • These snippets don't seem likely to be exactly the code you're executing. You should be getting rather different errors. 1) Directional quotations aren't valid delimiters for string literals. You should be seeing SyntaxErrors for those. 2) To require() another file in your project, the path should be relative to the current file. require('dashboard.js') expects to find node_modules/dashboard.js. Commented Apr 17, 2016 at 17:37
  • try: adding bracket. For example. dashboard.func1() Hope this work Commented Apr 17, 2016 at 17:40
  • Good observation , I have posted sudo code not working code , assume require('./dashboard.js') @JonathanLonowski Commented Apr 17, 2016 at 17:41
  • dashboard.func1() does not work , it gives same error @MichaelSeltene Commented Apr 17, 2016 at 17:42
  • 1
    @Sumeet If wrapping the call in another function works, does your application have a cycle (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. Commented Apr 17, 2016 at 17:45

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

why I am not able to add dashboard.func1 inside parallel
0

Please make sure the API path you are using should not have any return .. I have resolve this issue after calling API like this ... Please not i was facing this while working in local


fetchLearnMoreProxy: (isoCode, langCode) =>
    `http://localhost:3001/api?url=${hostName}/api/abc/cde?country=${isoCode}&language=${langCode}`,

Please let me know if any question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.