I have this small piece of example code:
//main.js
const {Worker} = require("worker_threads");
const worker = new Worker("./background.js", {workerData: {}});
function loop() {
console.log("main: 1234567890");
setTimeout(loop, 0)
}
loop();
And:
//background.js
const {parentPort, workerData} = require("worker_threads");
function loop () {
console.log("background: 1234567890");
setTimeout(loop, 0)
}
loop();
Run like so: node main.js
The output is this:
background: 1234567890
main: 1234567890
background: 1234567890
main: 1234567890
background: 1234567890
background: 1234567890
#etc
Sometimes several console.log()'s from one thread are called before any are called by the other. This is expected, and fine.
But is there any chance that a console.log() could be called in the middle of another console.log()? So for example, could this happen?
background: 123main: 12345456786789090
I haven't observed it in my output, but I'd like to know from a canonical source that Workers/console.log() do not work that way in node.js.