Skip to main content
Version: Unreleased

Running worklet functions on the UI runtime

The Worklets library provides three functions for running a worklet function on the UI Runtime from the RN Runtime: scheduleOnUI, runOnUIAsync and runOnUISync. They differ in whether the worklet function runs asynchronously or synchronously, and in what they return to the caller. Use the table below to pick the one that fits your case.

Comparison​

scheduleOnUIrunOnUIAsyncrunOnUISync
ExecutionAsynchronousAsynchronousSynchronous, blocks the caller until the worklet function returns
ReturnsNothingA Promise that resolves with the worklet function's return valueThe worklet function's return value directly
Web support✅✅❌
Use whenYou want to run a worklet function on the UI Runtime and don't need its result backYou need the worklet function's result back, but don't want to block the calling threadYou need the worklet function's result immediately, within the same synchronous flow

The values returned by runOnUIAsync and runOnUISync have to be serializable.

All three functions can be called from the RN Runtime, and additionally from the UI and Worker Runtimes when Bundle Mode is enabled. See Call tables for the full matrix.

Watch the execution​

Schedule work without waiting​

The UI thread runs the callback while the JS thread continues with the next line.

Code

Code

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

function myFunction() {
'worklet';
console.log('Hello from the UI Runtime!');
}
scheduleOnUI(myFunction);
console.log('The RN Runtime continues');

Await a result​

The callback runs on the UI thread. Awaiting its Promise suspends this function; the JS thread remains available for other queued work.

Code

Code

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

function compute() {
'worklet';
return 1;
}
const pending = runOnUIAsync(compute);
console.log('The RN Runtime continues while the result is pending');
const result = await pending;
console.log(result); // 1

Read a result synchronously​

The calling JS thread acquires the UI Runtime and executes the callback itself. It resumes the next line only after the result is ready.

Code

Code

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

function myFunction(num) {
'worklet';
return num + 1;
}
const result = runOnUISync(myFunction, 0);
console.log(result); // 1