UseRun
katanakit-js / useRun
Function: useRun()
Section titled “Function: 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 Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
TInput |
Input data type. |
TOutput |
Output data type. |
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<TOutput>
A Promise resolving to the function’s output.
Example
Section titled “Example”const doubled = await useRun((n: number) => n * 2, 21);console.log(doubled); // 42