Offload functions to worker threads with shared memory primitives for Node.js.
1import { setTimeout } from 'node:timers/promises';
2import { mo } from '../../src/index.ts';
3
4export const generate = mo(import.meta, async function* (n: number) {
5 for (let i = 0; i < n; i++) {
6 yield i;
7 }
8});
9
10export const process = mo(import.meta, async (input: AsyncIterable<number>): Promise<number[]> => {
11 const results: number[] = [];
12 for await (const n of input) {
13 await setTimeout(10); // simulate async work
14 results.push(n);
15 }
16 return results;
17});