Skip to main content
Version: Unreleased

runOnRuntimeAsync

Available from 0.8.0

runOnRuntimeAsync lets you asynchronously run worklet functions on a Worker Runtime. It returns a Promise of the worklet function's return value. The Promise is resolved asynchronously, not immediately after the callback execution.

Reference​

Code

Code

Colours connect each executing thread to its current code line. Recently executed lines fade over the next three ticks.

function sum(numbers) {
'worklet';
return numbers.reduce((total, n) => total + n, 0);
}
const workletRuntime = createWorkletRuntime({ name: 'background' });
const data = [1, 2, 3, 4];
const pending = runOnRuntimeAsync(workletRuntime, sum, data);
console.log('The RN Runtime continues while the worker computes');
const result = await pending;
console.log('Computation result:', result); // 10

Type definitions

function runOnRuntimeAsync<Args extends unknown[], ReturnValue>(
workletRuntime: WorkletRuntime,
worklet: (...args: Args) => ReturnValue,
...args: Args
): Promise<ReturnValue>;

Arguments​

workletRuntime​

The worklet runtime to run the worklet function on. Created with createWorkletRuntime.

worklet​

A reference to a function you want to execute on the Worker Runtime. Arguments to your function have to be passed after the worklet function i.e. runOnRuntimeAsync(workletRuntime, setValue, 10);.

args​

Arguments to the function you want to execute on the Worker Runtime.

Remarks​

  • It's a common mistake to execute function inside of runOnRuntimeAsync like this: runOnRuntimeAsync(workletRuntime, myWorklet(10)). Here, the correct usage would be runOnRuntimeAsync(workletRuntime, myWorklet, 10).

  • The callback passed as the argument is automatically workletized and ready to be run on the Worker Runtime.

  • Errors thrown in the worklet function will cause the Promise to reject with that error.

Call table​

ModeRN RuntimeUI RuntimeWorker Runtime
Bundle Mode✅✅✅
Legacy Eval Mode✅❌❌

What does it mean?

Platform compatibility​

NativeWeb
✅❌