ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – Node.js async parallel TypeError:task不是函数

2019-07-06 08:36:53  阅读:226  来源: 互联网

标签:javascript asynchronous node-js async-js


我正在使用异步模块来执行并行任务.基本上我有两个不同的文件,dashboard.js和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”);
   });

我期待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是功能?

解决方法:

对于异步属性,我会使用回调功能.此功能还有利于非阻塞呼叫.

使用您的代码,您可以尝试

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

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

在以下链接中还有一个与您类似的问题:Best way to execute parallel processing in Node.js

此外,错误的具体答案在以下链接中:
TypeError: task is not a function in async js parrallel

标签:javascript,asynchronous,node-js,async-js
来源: https://codeday.me/bug/20190706/1395783.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有