我正在使用 async 模块来执行并行任务。基本上,我有两个不同的文件,dashboard.js 和 Run.js。
仪表板.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.func1、dashboard.func2?
对于异步属性,我将使用回调功能。此功能还有益于非阻塞调用。
使用您的代码,您可以尝试
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);
}
});
});
还有一个与您类似的问题:Best way to executeparallelprocessing in Node.js
另外,该错误的具体答案在: 类型错误:任务不是异步 js 并行中的函数
请确保您使用的API路径不应该有任何返回..我在像这样调用API后解决了这个问题... 请不要,我在当地工作时遇到了这个问题
fetchLearnMoreProxy: (isoCode, langCode) =>
`http://localhost:3001/api?url=${hostName}/api/abc/cde?country=${isoCode}&language=${langCode}`,
如有任何问题请告诉我。