- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我决定使用 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/
我有一个“有趣”的问题,即以两种不同的方式运行 wine 会导致: $> wine --version /Applications/Wine.app/Contents/Resources/bin/wi
我制作了这个网络抓取工具来获取网页中的表格。我使用 puppeteer (不知道 crontab 有问题)、Python 进行清理并处理数据库的输出 但令我惊讶的是,当我执行它时 */50 * * *
JavaScript 是否被调用或执行取决于什么?准确地说,我有两个函数,它们都以相同的方式调用: [self.mapView stringByEvaluatingJavaScriptFromStri
我目前正在使用 python 做一个机器学习项目(这里是初学者,从头开始学习一切)。 只是想知道 statsmodels 的 OLS 和 scikit 的 PooledOlS 使用我拥有的相同面板数据
在使用集成对象模型 (IOM) 后,我可以执行 SAS 代码并将 SAS 数据集读入 .Net/C# 数据集 here . 只是好奇,使用 .Net 作为 SAS 服务器的客户端与使用 Enterpr
有一些直接的 jQuery 在单击时隐藏打开的 div 未显示,但仍将高度添加到导航中以使其看起来好像要掉下来了。 这个脚本工作正常: $(document).ready(funct
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 4 年前。 这里是 Java 新手,我正在使用 NetBeans 尝试一些简单的代
如果我将它切换到 Python 2.x,它执行 10。这是为什么? 训练逻辑回归模型 import keras.backend as
我有两个脚本,它们包含在 HTML 正文中。在第一个脚本中,我初始化一个 JS 对象,该对象在第二个脚本标记中引用。 ... obj.a = 1000; obj.
每当我运行该方法时,我都会收到一个带有数字的错误 以下是我的代码。 public String getAccount() { String s = "Listing the accounts";
我已经用 do~while(true) 创建了我的菜单;但是每次用户输入一个数字时,它不会运行程序,而是再次显示菜单!你怎么看? //我的主要方法 public static void main(St
执行命令后,如何让IPython通知我?我可以使用铃声/警报还是通过弹出窗口获取它?我正在OS X 10.8.5的iTerm上运行Anaconda。 最佳答案 使用最新版本的iTerm,您可以在she
您好,我刚刚使用菜单栏为 Swing 编写了代码。但是问题出现在运行中。我输入: javac Menu.java java Menu 它没有给出任何错误,但 GUI 没有显示。这是我的源代码以供引用:
我觉得这里缺少明显的东西,但是我看不到它写在任何地方。 我使用Authenticode证书对可执行文件进行签名,但是当我开始学习有关它的更多信息时,我对原样的值(value)提出了质疑。 签名的exe
我正在设计一个应用程序,它使用 DataTables 中的预定义库来创建数据表。我想对数据表执行删除操作,为此应在按钮单击事件上执行 java 脚本。 $(document).ready(functi
我是 Haskell 新手,如果有人愿意帮助我,我会很高兴!我试图让这个程序与 do while 循环一起工作。 第二个 getLine 命令的结果被放入变量 goGlenn 中,如果 goGlenn
我有一个用 swing 实现迷你游戏的程序,在主类中我有一个循环,用于监听游戏 map 中的 boolean 值。使用 while 实现的循环不会执行一条指令,如果它是唯一的一条指令,我不知道为什么。
我正在尝试开发一个连接到 Oracle 数据库并执行函数的 Java 应用程序。如果我在 Eclipse 中运行该应用程序,它可以工作,但是当我尝试在 Windows 命令提示符中运行 .jar 时,
我正在阅读有关 Java 中的 Future 和 javascript 中的 Promises 的内容。下面是我作为示例编写的代码。我的问题是分配给 future 的任务什么时候开始执行? 当如下行创
我有一个常见的情况,您有两个变量(xSpeed 和 ySpeed),当它们低于 minSpeed 时,我想将它们独立设置为零,并在它们都为零时退出。 最有效的方法是什么?目前我有两种方法(方法2更干净
我是一名优秀的程序员,十分优秀!