Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

console.log() is "Canceled" when looping unattended

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.

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).

The other two console.log() statements continue to output the correct values till the end of the loop.

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:

enter image description here

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.

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?

like image 356
Old Geezer Avatar asked Oct 14 '22 22:10

Old Geezer


1 Answers

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);
  })

enter image description here

The problem disappears when I set the breakpoint and print the data line by line.

The problem also disappears when I run the script in the powershell.

enter image description here

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.

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

I find someone else has a similar problem.
Weird node.js console.log() output behaviour when printing arrays

like image 118
yang piao Avatar answered Oct 19 '22 18:10

yang piao