簡體   English   中英

Node.js 異步並行“TypeError:任務不是函數”

[英]Node.js async parallel “TypeError: task is not a function”

我正在使用 async 模塊來執行並行任務。 基本上,我有兩個不同的文件,dashboard.js 和 Run.js。

儀表板.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.func2即使dashboard.func1是一個函數?

對於 async 屬性,我會使用回調功能。 此功能也有利於非阻塞調用。

用你的代碼,你可以試試

文件儀表板.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM