Skip to content

UseRun

katanakit-js / useRun

function useRun<TInput, TOutput>(workerFunc, data): Promise<TOutput>;

Defined in: infrastructure/worker/worker.service.ts:44

Runs a pure function in a one-shot Worker and destroys it afterwards. Falls back to running on the main thread when Workers are unavailable (SSR). Async worker functions are awaited (Promise resolved before postMessage).

Type Parameter Description
TInput Input data type.
TOutput Output data type.
Parameter Type Description
workerFunc WorkerFunc<TInput, TOutput> The pure function to execute off the main thread.
data TInput The input data passed to the function.

Promise<TOutput>

A Promise resolving to the function’s output.

const doubled = await useRun((n: number) => n * 2, 21);
console.log(doubled); // 42