Reputation: 12945
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?
Upvotes: 5
Views: 5130
Reputation: 133
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.
Upvotes: 0
Reputation: 591
For the async property, I would use the callback feature. This feature also benefits non-blocking calls.
With your code, you could try
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);
}
}
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
Upvotes: 2