Async.parallelLimit函数执行任务前的回调函数

Async.parallelLimit Function Executing Callback Function Before Tasks

我正在尝试为不同的环境并行设置一些 newman 测试 运行,然后在所有结果完成后通过 API 请求创建 Jira 票证。但是,当我为该任务使用 ansync.parallelLimit 函数时,我发现创建的 jira 票证没有来自测试 运行s.

的任何信息

这个过程的简化版本是这样的:

var async = require('async');
const path = require('path');
const newman = require('newman');
config = require(path.join(__dirname, 'config.json'));

const jira_data = [];

var create_ticket = function(err, results){
    console.log("Run Complete");
};

var test_run = async() =>{
  newman.run({
    collection: path.join(__dirname, 'Test.postman_collection.json'),
    reporters: ['htmlextra'],
    reporter: {
      htmlextra : { export : path.join(__dirname, '/reports/Test.html')}
    },})
}

var commands = [test_run,test_run,test_run,test_run];

async.parallelLimit(commands,4, (err, results) => {
  console.log("Run Complete");
  console.log(results);
});

这将导致以下输出,表明 create_ticket 在其他任何事情之前被执行:

Run Complete
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Created the htmlextra report.
Created the htmlextra report.
Created the htmlextra report.
Created the htmlextra report.

或者,我可以使 test_run 成为函数而不是异步,如下所示:

var async = require('async');
const path = require('path');
const newman = require('newman');

const jira_data = [];

var create_ticket = function(err, results){
    console.log("Test Run Complete");
};

var test_run = function() {
  newman.run({
    collection: path.join(__dirname, 'Test.postman_collection.json'),
    reporters: ['htmlextra'],
    reporter: {
      htmlextra : { export : path.join(__dirname, '/reports/Test.html')}
    },}).on('request', function(err, results) {
      console.log("Hello");
    })
}

var commands = [test_run, test_run, test_run, test_run];

async.parallelLimit(commands, 4, create_ticket);

但是现在,创建票证根本 运行:

Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Hello
Hello
Hello
Hello
Created the htmlextra report.
Created the htmlextra report.
Created the htmlextra report.
Created the htmlextra report.

在尝试变量类型的不同组合、参数传递和其他修复之后,我花了比我愿意承认的更多的时间,我完全被难住了。任何帮助都将不胜感激。

您需要向 test_run 传递并调用回调以完成异步任务。

var test_run = function(callback) {
  newman.run({
    collection: path.join(__dirname, 'Test.postman_collection.json'),
    reporters: ['htmlextra'],
    reporter: {
      htmlextra : { export : path.join(__dirname, '/reports/Test.html')}
    },}).on('request', function(err, results) {
      console.log("Hello");
      callback(null);
    })
}