node.js - 无人值守循环时,console.log() 为 "Canceled"

标签 node.js visual-studio-code

我能够重现我在 VS Code 的 Node 项目中遇到的问题,如下所示:

for (let i=0; i<50; i++) {
  let text = [ Math.random()>0.5 ? "abc" : "ABC" ];
  console.log(i);
  console.log(text);
  console.log("*")
}
如果我手动通过,一切正常。
如果我让脚本全速运行,随机迭代,红色的“取消”字样会在第一个 console.log 之后打印出来。打印其输出。从那时起,第二个 console.log(text)被跳过或输出一个空白字符串(我不知道是哪个)。
另外两个console.log()语句继续输出正确的值直到循环结束。
“已取消”的颜色与我 console.error() 的输出相同.
示例运行如下所示,其中在第七次迭代后发生异常:
enter image description here
我多次重复脚本,每次“取消”输出发生在不同的迭代中。如果脚本中的任何地方(不一定在循环中)有断点,则“取消”不会发生。
尽管输出错误,但所有变量的值似乎都是正确的。在我的实际代码中,我保存了 text 的最终值。到一个文件,他们正确 checkout 。
发生了什么,有什么需要担心的吗?

最佳答案

我也有同样的问题。我认为这是某种 VSCode 错误。
当我打印 data在事件回调中,console.log按预期行事。但是过了一会儿,主线程存在并且console.log输出 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);
  })
enter image description here
当我设置断点并打印 data 时,问题就消失了。逐行。
当我在 powershell 中运行脚本时,问题也消失了。
enter image description here
虽然console.log被取消,data已成功推送到feature list .该行为很可能是由 vscode 终端或 vscode 处理 console.log 的方式引起的。命令。
我的 Node 版本是 v12.20.0,VSCode 版本是:
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
我发现其他人也有类似的问题。
Weird node.js console.log() output behaviour when printing arrays

关于node.js - 无人值守循环时,console.log() 为 "Canceled",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65914346/

相关文章:

javascript - 使用 node.js 进行长轮询并表达 : how to cache res object into redis or other cache framework

Node.js 应用程序 - 将 mongodb 集成到 appfog 中

javascript - 回调解决未处理的promise,为什么?

node.js - 如何将 Angular cli 项目与 nodejs/expressjs 项目集成

python - 使用 pandasql 时在 VS Code 中使用多个制表符间距设置和注释

javascript - VS Code 在 React 测试用例中使用 require 语法导入

typescript - 版本不匹配 global tsc 1.0.3.0 != VS Code 的语言服务 (2.2.1)

visual-studio-code - 如何在 Visual Studio 代码中使用 Autohotkey 热字符串?

node.js - 错误 : Failed to lookup view "layout" in views directory

visual-studio-code - 如何通过vscode在jupyter中显示图像