Skip to content
0

katanakit-js / useRun

Function: useRun() ​

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

Defined in: src/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 Parameters ​

Type ParameterDescription
TInputInput data type.
TOutputOutput data type.

Parameters ​

ParameterTypeDescription
workerFuncWorkerFunc<TInput, TOutput>The pure function to execute off the main thread.
dataTInputThe input data passed to the function.

Returns ​

Promise<TOutput>

A Promise resolving to the function's output.

Example ​

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

Released under the MIT License.