Node.js c parallel“TypeError:任务不是函数”

mrzz3bfm  于 12个月前  发布在  Node.js
关注(0)|答案(2)|浏览(93)

我正在使用BRAC模块来执行并行任务。基本上,我有两个不同的文件,dashboard.js和Run.js。

Dashboard.js

module.exports = {

    func1 : function(){
        console.log(“Funtion one”);

    },
    func2 : function(){
        console.log(“Funtion two”);
    }

}

运行.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

rqmkfv5c

rqmkfv5c1#

对于Cashc属性,我将使用回调特性。此功能也有利于非阻塞呼叫。
使用您的代码,您可以尝试

文件 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);
   }
}

文件 * 运行.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);
     }
   });
});

还有一个问题和你的类似:Best way to execute parallel processing in Node.js
此外,错误的具体答案是:TypeError: task is not a function in async js parrallel

pvabu6sv

pvabu6sv2#

请确保您正在使用的API路径不应该有任何返回。我这样调用API后解决了这个问题.请不要我在当地工作时就面临着这一点

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

请让我知道如果任何问题。

相关问题