A second kitchen
A worker thread adds up ten million numbers. Watch what the main thread does meanwhile.
const { Worker } = require('node:worker_threads');const w = new Worker(` const { parentPort } = require('node:worker_threads'); let sum = 0; for (let i = 0; i < 1e7; i++) sum += i; parentPort.postMessage(sum);`, { eval: true });w.on('message', (sum) => console.log('worker says ' + sum));setTimeout(() => console.log('timer'), 0);console.log('main keeps going');1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…