Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 24 lines 752 B view raw
1// Fan-out a single channel to multiple workers using work stealing. 2// Each item goes to whichever worker is ready first. 3// Requires Node v24+. 4// 5// Run: node examples/channel-fanout/main.ts 6 7import { workers, channel, assign } from '../../src/index.ts'; 8import { generate, process } from './work.ts'; 9 10{ 11 using run = workers(); 12 const ch = channel(generate(200)); 13 const fanout = run.workers.map((w) => { 14 return assign(w, process(ch)); 15 }); 16 const results: number[][] = await run(fanout); 17 18 for (let i = 0; i < results.length; i++) { 19 console.log(`Worker ${i}: processed ${results[i].length} items`); 20 } 21 22 const all = results.flat().sort((a, b) => a - b); 23 console.log(`\nTotal: ${all.length} items, none lost, none duplicated`); 24}