// Demonstrates that the main thread event loop stays responsive // while heavy work runs on worker threads. // Requires Node v24+. // // Run: node examples/non-blocking/main.ts import { workers } from '../../src/index.ts'; import { fibonacci } from './fibonacci.ts'; // Tick a counter on the main thread to prove it's not blocked let ticks = 0; const interval = setInterval(() => { ticks++; process.stdout.write(`\r main thread tick #${ticks}`); }, 100); console.log('Computing fibonacci(42) on a worker pool...'); console.log('Meanwhile, the main thread keeps ticking:\n'); { using run = workers(2); const start = performance.now(); const [a, b] = await run([fibonacci(42), fibonacci(41)]); const elapsed = (performance.now() - start).toFixed(0); clearInterval(interval); console.log(`\n\nResults: fib(42)=${a}, fib(41)=${b}`); console.log(`Computed in ${elapsed}ms with ${ticks} main-thread ticks (not blocked!)`); }