···11+// Moroutines from different modules running on a single worker.
22+// The worker lazily imports each module the first time a task from it is dispatched.
33+// Requires Node v24+.
44+//
55+// Run: node examples/multi-module/main.ts
66+77+import { workers } from '../../src/index.ts';
88+import { square, add } from './math.ts';
99+import { uppercase, repeat } from './text.ts';
1010+1111+{
1212+ using run = workers(1);
1313+1414+ // All four moroutines from two modules run on the same single worker
1515+ const results = await Promise.all([
1616+ run(square(7)),
1717+ run(add(3, 4)),
1818+ run(uppercase('hello')),
1919+ run(repeat('ab', 3)),
2020+ ]);
2121+2222+ console.log('square(7) =', results[0]); // 49
2323+ console.log('add(3, 4) =', results[1]); // 7
2424+ console.log("uppercase('hello') =", results[2]); // HELLO
2525+ console.log("repeat('ab', 3) =", results[3]); // ababab
2626+}
+9
examples/multi-module/math.ts
···11+import { mo } from '../../src/index.ts';
22+33+export const square = mo(import.meta, (n: number): number => {
44+ return n * n;
55+});
66+77+export const add = mo(import.meta, (a: number, b: number): number => {
88+ return a + b;
99+});