NodeJS创建函数数组,通过async.series执行

NodeJS Create an array of functions and execute it through async.series

开始学习 NodeJS,我尝试使用 async.series 调用一系列函数。每个函数都是一个命令行,使用 REST 远程调用。

function TestStorageDeviceBasic()
{
    scenario = [
        'cmdline1',
        'cmdline2'
    ];

    tasks = [];
    scenario.forEach(command => { tasks.push(RunCommand(sessionId,command));});
    async.series(tasks);
}

function RunCommand(sessionId, cmdLine)
{
    var options = {
        uri: `http://${domain}/api/v1/commands`,
        method: 'POST',
        json: {
            'session-id' : `${sessionId}`,
            'command-line': `${cmdLine}` 
        }
      };

      request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            log.debug(`\"${cmdLine}\" status code successful`)
            log.debug(body);
        }
        else
            log.error(`\"${cmdLine}\" status code failed`,error);
      });
}

尽管似乎调用了 RunCommand 函数,但我遇到了一些问题。

(node:21008) UnhandledPromiseRejectionWarning: Error: expected a function
    at wrapAsync (C:\work\MyJavascriptProject\my_sample\node_modules\async\dist\async.js:198:50)
    at C:\work\MyJavascriptProject\my_sample\node_modules\async\dist\async.js:2952:13

为什么 RunCommand 不被视为函数?

这是因为您正在 调用 RunCommand,然后将其 return 值推入 tasks。相反,推送一个调用的函数:

scenario.forEach(command => { 
    tasks.push(() => RunCommand(sessionId,command));
});

代码中需要修改三处

  • 将 RunCommand 包装在一个函数中,然后将其推送到任务数组中
  • 确保在结束时传递回调
  • 修改 RunCommand 使其也具有回调,以便 async.series 可以使用回调的输出。

由于您没有为 RunCommand 传递回调,因此它只执行一次,因为 async.series 不知道何时继续。修改后的代码看起来像

function TestStorageDeviceBasic() {
  scenario = ["cmdline1", "cmdline2"];

  tasks = [];
  scenario.forEach(command => {
    tasks.push(callack => RunCommand(sessionId, command, callack));
  });
  async.series(tasks, (err, data) => {
    if (err) {
      console.error(err);
    }
    console.log(data);
  });
}

function RunCommand(sessionId, cmdLine, callack) {
  var options = {
    uri: `http://${domain}/api/v1/commands`,
    method: "POST",
    json: {
      "session-id": `${sessionId}`,
      "command-line": `${cmdLine}`
    }
  };

  request(options, function(error, response, body) {
    if (!error && response.statusCode == 200) {
      log.debug(`\"${cmdLine}\" status code successful`);
      log.debug(body);
      callack(null, body);
    } else {
      log.error(`\"${cmdLine}\" status code failed`, error);
      callack(error, null);
    }
  });
}

希望对您有所帮助