Very simple worker thread abstraction for ts-node ESM builds.
npm install threads-esmVery simple worker thread abstraction for ts-node ESM builds.
Couldn't get ts-node ESM workers to work with threads.js, and it doesn't look like I'm alone.
I threw this together as a quick alternative for what I needed.
``bash`
yarn add threads-esm
or
`bash`
npm install threads-esm
This basic example can be run using the example script (yarn example or npm run example).
`ts`
interface ExampleWorker {
add: (a: number, b: number) => Promise
subtract: (a: number, b: number) => number;
foo: string;
}
`ts
import { spawn } from 'threads-esm';
const worker = await spawn
// an async function that returns the result of the add function
const result1 = await worker.add(1, 2);
// an async function that returns the result of the subtract function
const result2 = await worker.subtract(1, 2);
// an async function that returns the value of foo property
const result3 = await worker.foo();
`
`ts
import { expose } from 'threads-esm';
expose
// A function that returns a promise of a number
add: (a: number, b: number): Promise
// A function that returns a number
subtract: (a: number, b: number): number => a - b,
// A property that is a string
foo: 'bar'
});
``