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:
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?
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);
})
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With