console.log() 已“取消";循环无人看管时 [英] console.log() is "Canceled" when looping unattended
问题描述
我可以使用以下代码重现我在 VS Code 的 Node 项目中遇到的问题:
I am able to reproduce a problem I am facing in a Node project in VS Code with the following:
for (let i=0; i<50; i++) {
let text = [ Math.random()>0.5 ? "abc" : "ABC" ];
console.log(i);
console.log(text);
console.log("*")
}
如果我手动执行,一切正常.
If I step through manually, everything works fine.
如果我让脚本全速运行,随机迭代,红色的取消"字样就会出现.将在第一个 console.log
打印其输出后打印.从那时起,第二个 console.log(text)
被跳过或输出一个空白字符串(我无法分辨哪个).
If I let the script run at full speed, at a random iteration, the words in red "Canceled" would be printed after the first console.log
prints its output. From that point onward, the second console.log(text)
is skipped or outputs a blank string (I am unable to tell which).
另外两个 console.log()
语句继续输出正确的值,直到循环结束.
The other two console.log()
statements continue to output the correct values till the end of the loop.
取消"的颜色与我的 console.error()
的输出相同.
The color of the "Canceled" is the same as the output of my console.error()
.
示例运行如下所示,其中在第七次迭代后发生异常:
A sample run is as shown here where the anomaly happens after the seventh iteration:
我多次重复这个脚本,每次取消"都被取消了.输出发生在不同的迭代中.如果脚本中的任何地方(不一定在循环中)有断点,则Canceled"将被取消.不会发生.
I repeated the script many times, and each time the "Canceled" output happens at a different iteration. If there is a breakpoint anywhere in the script, not necessarily in the loop, the "Canceled" does not happen.
尽管输出错误,但所有变量的值似乎都是正确的.在我的实际代码中,我将 text
的最终值保存到一个文件中,并且它们被正确检出.
Despite the erroneous output, the values of all variables seem to be correct. In my real code, I saved the final values of text
to a file and they checked out correctly.
发生了什么,有什么需要担心的吗?
What is happening, and is there anything to worry about?
推荐答案
我有同样的问题.我认为这是某种 VSCode 错误.
当我在事件回调中打印 data
时,console.log
会按预期运行.但是过了一会儿,主线程存在并且console.log
输出Canceled
.
I have the same problem. I think it is some kind of VSCode bug.
When I print the data
in a event callback, console.log
behave as expected. But after a while, the main thread exists and console.log
output Canceled
.
import some modules...
vtgeojson(tiles, { tilesOnly: true })
.on('data', function (data) {
featurecollection.features.push(data);
console.log(data)
})
.on('end', function () {
console.log(featurecollection);
console.log(featurecollection.features.length);
})
当我设置断点并逐行打印data
时,问题就消失了.
The problem disappears when I set the breakpoint and print the data
line by line.
当我在 powershell 中运行脚本时,问题也消失了.
The problem also disappears when I run the script in the powershell.
虽然取消了console.log
,但是data
已经成功推送到feature list
.该行为很可能是由 vscode 终端或 vscode 处理 console.log
命令的方式引起的.
Although the console.log
is canceled, the data
has been successfully pushed into the feature list
. The behavior is most likely caused by the vscode terminal or the way that vscode handle the console.log
command.
我的node版本是v12.20.0,VSCode版本是:
My node version is v12.20.0 and VSCode version is:
Version: 1.51.1 (system setup)
Commit: e5a624b788d92b8d34d1392e4c4d9789406efe8f
Date: 2020-11-10T23:34:32.027Z
Electron: 9.3.3
Chrome: 83.0.4103.122
Node.js: 12.14.1
V8: 8.3.110.13-electron.0
OS: Windows_NT x64 10.0.17763
我发现其他人也有类似的问题.
奇怪的 node.js console.log() 输出行为打印数组
I find someone else has a similar problem.
Weird node.js console.log() output behaviour when printing arrays
这篇关于console.log() 已“取消";循环无人看管时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!