javascript - 尽管使用了 Async 模块,为什么这些函数仍然异步执行

标签 javascript node.js mongodb asynchronous async.js

我决定使用 Async 模块按照我想要的顺序填充一个 mongodb 集合。
如果没有 Async,代码可以工作,但文档不会以正确的顺序插入:

function insertRowInBLD(ref, riskstatements, maximpact, controleffectiveness, recommendedriskrating, frequency, impact, validatedreviewriskrating, rationalforriskadjustment) {
    const businessLineDashboard = new BusinessLineDashboard({
        ref: ref,
        riskstatements: riskstatements,
        maximpact: maximpact,
        controleffectiveness: controleffectiveness,
        recommendedriskrating: recommendedriskrating,
        frequency: frequency,
        impact: impact,
        validatedreviewriskrating: validatedreviewriskrating,
        rationalforriskadjustment: rationalforriskadjustment
    });
    businessLineDashboard.save()
        .then(row => {
            console.log('row ' + businessLineDashboard.ref + ' has been inserted succesfully');
        })
        .catch(err => {
            console.log('err: ', err);
        });
}

我希望按该顺序插入“文档”。由于 JavaScript 的异步特性,这并没有发生。所以我尝试使用

异步系列:

function fillBLD() {


  async.series([
    function (callback) {
      console.log("Task 1");
      insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 1 Inserted');
    },
    function (callback) {
      console.log("Task 2");
      insertRowInBLD('R02', 'Corruption of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 2 Inserted');
    },
    function (callback) {
      console.log("Task 3");
      insertRowInBLD('R03', 'Unavailability of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', '', '', '', '', '')
      callback(null, 'Row 3 Inserted');
    },
    function (callback) {
      console.log("Task 4");
      insertRowInBLD('R04', 'Disclosure of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 4 Inserted');
    },
    function (callback) {
      console.log("Task 5");
      insertRowInBLD('R05', 'Corruption of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 5 Inserted');
    },
    function (callback) {
      console.log("Task 6");
      insertRowInBLD('R06', 'Unavailability of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 6 Inserted');
    },
    function (callback) {
      console.log("Task 7");
      insertRowInBLD('R07', 'Disclosure of data due to social engineering by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 7 Inserted');
    },
    function (callback) {
      console.log("Task 8");
      insertRowInBLD('R08', 'Corruption of data due to social engineering by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 8 Inserted');
    },
    function (callback) {
      console.log("Task 9");
      insertRowInBLD('R09', 'Unavailability of data due to social engineering by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 9 Inserted');
    },
    function (callback) {
      console.log("Task 10");
      insertRowInBLD('R10', 'Disclosure of data due to  erroneous useby internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 10 Inserted');
    },
    function (callback) {
      console.log("Task 11");
      insertRowInBLD('R11', 'Corruption of data due to erroneous useby internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 11 Inserted');
    },
    function (callback) {
      console.log("Task 12");
      insertRowInBLD('R12', 'Unavailability of data due to erroneous useby internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 12 Inserted');
    },
    function (callback) {
      console.log("Task 13");
      insertRowInBLD('R13', 'Disclosure of data due to  unauthorized access by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 13 Inserted');
    },
    function (callback) {
      console.log("Task 14");
      insertRowInBLD('R14', 'Corruption of data due to unauthorized access by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 14 Inserted');
    },
    function (callback) {
      console.log("Task 15");
      insertRowInBLD('R15', 'Unavailability of data due to unauthorized access by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 15 Inserted');
    },
    function (callback) {
      console.log("Task 16");
      insertRowInBLD('R16', 'Disclosure of data due to  attack by malicious code by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 16 Inserted');
    },
    function (callback) {
      console.log("Task 17");
      insertRowInBLD('R17', 'Corruption of data due to attack by malicious code by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 17 Inserted');
    },
    function (callback) {
      console.log("Task 18");
      insertRowInBLD('R18', 'Unavailability of data due to erroneous useby internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 18 Inserted');
    },
    function (callback) {
      console.log("Task 19");
      insertRowInBLD('R19', 'Disclosure of data due to improper change/maintenance by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 19 Inserted');
    },
    function (callback) {
      console.log("Task 20");
      insertRowInBLD('R20', 'Corruption of data due to improper change/maintenance by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 20 Inserted');
    },
    function (callback) {
      console.log("Task 21");
      insertRowInBLD('R21', 'Unavailability of data due to improper change/maintenance by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 21 Inserted');
    },
    function (callback) {
      console.log("Task 22");
      insertRowInBLD('R22', 'Disclosure of data due to loss or theft of device by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 22 Inserted');
    },
    function (callback) {
      console.log("Task 23");
      insertRowInBLD('R23', 'Unavailability of data due to loss or theft of device by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 23 Inserted');
    },
    function (callback) {
      console.log("Task 24");
      callback(null, 'Row 24 Inserted');
    },
    function (callback) {
      console.log("Task 25");
      insertRowInBLD('R25', 'Corruption of data due to bypassing physical security by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')

      callback(null, 'Row 25 Inserted');
    },
    function (callback) {
      console.log("Task 26");
      insertRowInBLD('R26', 'Unavailability of data due to bypassing physical security by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 26 Inserted');
    },
    function (callback) {
      console.log("Task 27");
      insertRowInBLD('R27', 'Disclosure of data due to third-party security breach by external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 27 Inserted');
    },
    function (callback) {
      console.log("Task 28");
      insertRowInBLD('R28', 'Corruption of data due to  third-party security breach by external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 28 Inserted');
    },
    function (callback) {
      console.log("Task 29");
      insertRowInBLD('R29', 'Unavailability of data due to third-party security breach by external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 29 Inserted');
    },
    function (callback) {
      console.log("Task 30");
      insertRowInBLD('R30', 'Disclosure of data due to unmanaged legal, regulatory and contractual requirements by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 30 Inserted');
    },
    function (callback) {
      console.log("Task 31");
      insertRowInBLD('R31', 'Corruption of data due to unmanaged legal, regulatory and contractual requirements by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 31 Inserted');
    },
    function (callback) {
      console.log("Task 32");
      insertRowInBLD('R32', 'Unavailability of data due to unmanaged legal, regulatory and contractual requirements by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')

      callback(null, 'Row 32 Inserted');
    },
    function (callback) {
      console.log("Task 33");
      callback(null, 'Row 33 Inserted');
    },
    function (callback) {
      console.log("Task 33");
      insertRowInBLD('R33', 'Unavailability of data due to component failure by internal/external factor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')

      callback(null, 'Row 33 Inserted');
    },
    function (callback) {
      console.log("Task 34");
      insertRowInBLD('R34', 'Unavailability of data due to exhaustion of resources by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 34 Inserted');
    },
    function (callback) {
      console.log("Task 35");
      insertRowInBLD('R35', 'Unavailability of data due to environmental & natural disasters by external factor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')

      callback(null, 'Row 35 Inserted');
    },
    function (callback) {
      console.log("Task 36");
      insertRowInBLD('R36', 'Lack of accountability due to tampering with audit trails by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
      callback(null, 'Row 36 Inserted');
    },
  ], function (error, results) {
    console.log(results);
  });
}

但是,当控制台日志同步执行并且结果按顺序传递给回调函数时:

Task 1 Task 2 Task 3 Task 4 Task 5 Task 6 Task 7 Task 8 Task 9 Task 10 Task 11 Task 12 Task 13 Task 14 Task 15 Task 16 Task 17 Task 18 Task 19 Task 20 Task 21 Task 22 Task 23 Task 24 Task 25 Task 26 Task 27 Task 28 Task 29 Task 30 Task 31 Task 32 Task 33 Task 33 Task 34 Task 35 Task 36 [ 'Row 1 Inserted', 'Row 2 Inserted', 'Row 3 Inserted', 'Row 4 Inserted', 'Row 5 Inserted', 'Row 6 Inserted', 'Row 7 Inserted', 'Row 8 Inserted', 'Row 9 Inserted', 'Row 10 Inserted', 'Row 11 Inserted', 'Row 12 Inserted', 'Row 13 Inserted', 'Row 14 Inserted', 'Row 15 Inserted', 'Row 16 Inserted', 'Row 17 Inserted', 'Row 18 Inserted', 'Row 19 Inserted', 'Row 20 Inserted', 'Row 21 Inserted', 'Row 22 Inserted', 'Row 23 Inserted', 'Row 24 Inserted', 'Row 25 Inserted', 'Row 26 Inserted', 'Row 27 Inserted', 'Row 28 Inserted', 'Row 29 Inserted', 'Row 30 Inserted', 'Row 31 Inserted', 'Row 32 Inserted', 'Row 33 Inserted', 'Row 33 Inserted', 'Row 34 Inserted', 'Row 35 Inserted', 'Row 36 Inserted' ]

insertRowInBLD 函数仍然没有按照我定义的顺序执行:

row R01 has been inserted succesfully row R02 has been inserted succesfully row R03 has been inserted succesfully row R04 has been inserted succesfully row R05 has been inserted succesfully row R07 has been inserted succesfully row R08 has been inserted succesfully row R09 has been inserted succesfully row R06 has been inserted succesfully row R12 has been inserted succesfully row R19 has been inserted succesfully row R14 has been inserted succesfully row R17 has been inserted succesfully row R22 has been inserted succesfully row R28 has been inserted succesfully row R33 has been inserted succesfully row R25 has been inserted succesfully row R30 has been inserted succesfully row R35 has been inserted succesfully row R10 has been inserted succesfully row R15 has been inserted succesfully row R20 has been inserted succesfully row R26 has been inserted succesfully row R31 has been inserted succesfully row R36 has been inserted succesfully row R11 has been inserted succesfully row R16 has been inserted succesfully row R21 has been inserted succesfully row R27 has been inserted succesfully row R32 has been inserted succesfully row R13 has been inserted succesfully row R18 has been inserted succesfully row R23 has been inserted succesfully row R29 has been inserted succesfully row R34 has been inserted succesfully

我真的不明白为什么它们仍然异步执行。 知道是什么原因造成的吗?我该如何解决?
谢谢你!

最佳答案

您当前正在立即调用回调,而不是等待插入完成。这意味着您将立即开始所有保存,并且无法控制它们何时完成。相反,您希望在继续下一个之前等待一个完成,为此您需要使用由 businessLaneDashboard.save() 创建的 promise 。特别是,您需要从 insertRowInBLD 返回它:

function insertRowInBLD(/* args */) {
    const businessLineDashboard = new BusinessLineDashboard(
      //etc
    );
    return businessLineDashboard.save();
}

有了 promise,您可以使用它的 .then 方法在调用回调之前等待。

    function (callback) {
      console.log("Task 1");
      insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
        .then(() => callback(null, 'Row 1 Inserted'));
    },

虽然现在我们正在使用 promises,但我会放弃 async.series 的东西,只使用一个 promises 链,如:

insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
  .then(() => {
    return insertRowInBLD('R02', 'Corruption of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
  })
  .then(() => {
    return insertRowInBLD('R03', 'Unavailability of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', '', '', '', '', '')
  })
  // etc

如果 async/await 是您的选择,那么使用 promise 可以变得更加简单:

async function fillBLD() {
  await insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '');
  await insertRowInBLD('R02', 'Corruption of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '');
  await insertRowInBLD('R03', 'Unavailability of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', '', '', '', '', '');
  // etc
}

关于javascript - 尽管使用了 Async 模块,为什么这些函数仍然异步执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787897/

相关文章:

javascript - 如何使用 SystemJS 加载命名导出

javascript - 试图在印度 map 上应用等值线

android - 使用 parse-server-example 和 parse-dashboard(本地)连接一个新的 Android 项目

javascript - 如何从 mongodb shell 准备打印精美的 CSV?

javascript - 如何使用 JavaScript 计算 onLoad 和 onChange?

javascript - 需要在同一页面上的 "page load"之后触发警报

javascript - 如何像google Drive一样在网页中查看office文件

javascript - 无法从子进程获取管道流

javascript - 如何在nodejs中使用url获取文件大小

mongodb - Mongos认证