Saltar al contenido principal

Function: useRun()

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

TInput

TInput

Input data type.

TOutput

TOutput

Output data type.

Parameters

workerFunc

WorkerFunc<TInput, TOutput>

The pure function to execute off the main thread.

data

TInput

The input data passed to the function.

Returns

Promise<TOutput>

A Promise resolving to the function's output.

Example

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