Javascript 从动态生成的函数数组nodeJs异步

Javascript 从动态生成的函数数组nodeJs异步,javascript,arrays,node.js,asynchronous,Javascript,Arrays,Node.js,Asynchronous,因此,我正在处理从cli运行更新任务的nodejs任务 我的一段代码 var npm, grunt, async, path, which, color, isRoot, progressBar, EventEmitter, exec, utils, format; function loadDependencies() { npm = require('npm'); grunt = require('grunt'); async =

因此,我正在处理从cli运行更新任务的nodejs任务

我的一段代码

var
  npm,
  grunt,
  async,
  path,
  which,
  color,
  isRoot,
  progressBar,
  EventEmitter,
  exec,
  utils,
  format;

function loadDependencies() {
  npm = require('npm');
  grunt = require('grunt');
  async = require('async');
  path = require('path');
  which = require('which');
  color = require('cli-color');
  isRoot = require('is-root');
  progressBar = require('progress');
  EventEmitter = require("events").EventEmitter;
  exec = require('child_process').exec;
  utils = require('../utils');
  format = require('util').format
}

// Expose my module
module.exports = update;

function update(args, options, callback, ee){
  //Load all my deps 
  loadDeps();
  // Create my array
  var taskCollection = [];
  // Convert the update object with its functions into an array
  for(var tasks in update){
      taskCollection.push('update.' + tasks);
  }
**玩过之后,我决定尝试在这里添加以下数组**

taskCollection = [update.one, update.two];
并且它按它应该的方式运行任务,因此我现在只能假设我是如何制作阵列的

  //And this is were im stuck *
  async.series(taskCollection,
    function(err,result){
    console.log(error);
    console.log(result);
  });

}

update.one = function(callback){
    callback(null, 'Update One Complete);
}

update.two = function(callback){
    callback(null, 'Update Two Complete);
}
现在的想法是,异步运行的任务列表是从我的更新对象自动填充的,这意味着

update.bower = function(){}
update.grunt = function(){}
将由async运行,但其他任务

var getVersion = function(){}
当我想在我创建的任务数组上异步运行时,问题会出现吗?我不断得到错误,task()不是一个函数

我想用

async.series(taskCollection, finish);
还有某种形式的变化

async.each(taskCollection);
最终的目标是添加一个进度条,这样每个函数的回调函数都将调用progress.tick(),并从任务列表中自动计算刻度数

前进的最佳途径是什么? 哪一个异步(NodeJs版本)任务是最好的系列任务,因为它们是一个函数数组,所以我希望它们按顺序排列

提前谢谢你把我的头撞在墙上

更新

错误是从async.js:718生成的,并表示

task(_restParam(function (err,args))) {
^
TypeError: task is not a function

我向async传递的是字符串数组,而不是对象数组

考虑以下几点:

console.log(update);
// And you get
{[Function: update] one: [Function], two [Function]}
for (var obj in tasks){
    taskCount.push(obj);
    taskCollection.push(tasks[obj]);
}
所以在我上面的代码中

// Translation - For all the keys in the update object
for(var tasks in update){
  // push that key as a string :(
  taskCollection.push('update.' + tasks);
 }
将的更新为以下内容:

console.log(update);
// And you get
{[Function: update] one: [Function], two [Function]}
for (var obj in tasks){
    taskCount.push(obj);
    taskCollection.push(tasks[obj]);
}
现在taskCount将生成一个函数名数组
而taskCollection在传递给async:)时会起作用。

听起来您需要声明一个名为
task
的函数,或者传递一个具有该名称的函数的内容。代码到底在哪里断开?在上面的代码中,如果我被卡住了,我会调用async series,所以“async.series(taskCollection,finish);”完成获取错误/结果/回调的函数我检查了数组,如果我使用console.log(taskCollection),我会得到['update.one,update.two'],这意味着async应该运行update.one,所以我不理解您的意思。你能给你的问题添加一个堆栈跟踪吗?更新了更多的代码,我得到的错误也面临同样的问题。你能用一些值来详细说明这个答案吗?这个问题已经2年了,我记得我遇到过这个问题,但现在我的知识更丰富了,对于es6,我可以找到多种方法来解决我上面的代码可能导致的多个变化。例如,async.series接受一个数组或对象,这意味着可以使用以下语句声明函数:`async.series([()=>{//First Function},()=>{//Second Function}],callbackbunceton(allErr,allResults){})还有一个事实,就是我试图取笑该系列的对象的格式不正确。我会尝试用答案更新这篇文章