- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
今天,我正在为一个有关流动民主的项目使用循环函数。在委托(delegate)投票时,我有一个 while 循环来检查是否忘记了任何委托(delegate),该算法有一个有趣的行为:
Vote.count({
propId: propId,
delegation: true
}, function(err, voteNumber) {
var maxDelegation = voteNumber;
for (var d = 1; d < maxDelegation + 1; d++) {
var delegationsLeft = 1; //Is calculated to see if someone delegates to a delegate
while (delegationsLeft > 0) {
console.log("I am about to find the votes for " + d + " weight, " + delegationsLeft + " delegation left at least.");
Vote.find({
propId: propId,
delegation: true,
weight: d
}, function(err, specWeightVotes) {
console.log("I'm in.");
delegationsLeft = specWeightVotes.length;
for (var i = 0; i < specWeightVotes.length; i++) {
// Add the weight d, aka the weight of a vote to the delegated voter
Vote.findOneAndUpdate({
propId: propId,
voter: specWeightVotes[i].content
}, {
$inc: {
'weight': specWeightVotes[i].weight
}
}, function(err) {
if (err) {
console.log("Ca bug.");
}
//Put weight as zero for the ones who have delegated
Vote.findOneAndUpdate({
_id: specWeightVotes[i]._id
}, {
$set: {
'weight': 0
}
}, function(err) {
if (err) {
console.log("Ca bug 2.");
}
});
});
}
});
}
}
console.log("I have delegated all votes");
});
现在有趣的是,我的 while 循环永远卡住了,因为它永远无法执行 Vote.find({propId: propId, delegate: true, Weight: d}, function(err, specWeightVotes) {
部分代码。
如果我查看控制台,我会得到以下输出:
I am about to find the votes for 1 weight, 1 delegation left at least.
I am about to find the votes for 1 weight, 1 delegation left at least.
I am about to find the votes for 1 weight, 1 delegation left at least.
I am about to find the votes for 1 weight, 1 delegation left at least.
I am about to find the votes for 1 weight, 1 delegation left at least.
...直到最终进程耗尽电池。我从来没有看到“我在”的部分。
我想这可以通过更多的异步编程来解决?但是,考虑到我的函数甚至不执行一次,我不确定。
PS:重要提示,在包含此 while 循环以防止忘记委托(delegate)之前,算法是有效的。
最佳答案
大概Vote.find
异步地完成工作(对于Vote.findOneAndUpdate
也类似)。您的 for
和 while
循环不会。因此,在循环过程中,delegationsLeft
永远不会是 1。
如果您与异步结果交互,则不能使用 while
(或 for
或 do-while
)。相反,您需要从异步回调中安排“循环”的下一个“迭代”。
(或者将所有代码放入 ES2017+ async
函数中,将 Vote.find
等包装在返回 Promises 的包装器中,并使用 await
。但这需要一个尖端的 JavaScript 环境来运行,或者使用 Babel 之类的东西进行转译。)
关于javascript - 虽然函数陷入循环,但循环的一半似乎没有执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48184135/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!