Skip to main content
Version: 0.13

runOnRNSync

Available from 0.14.0

runOnRNSync lets you run a function synchronously on the RN Runtime from any runtime and get its return value. The calling thread waits until the JavaScript thread finishes running the function.

Reference​

import { runOnRNSync, scheduleOnUI } from 'react-native-worklets';

const getUserName = (): string => store.user.name;

scheduleOnUI(() => {
'worklet';
const name = runOnRNSync(getUserName);
console.log(name);
});

Type definitions

function runOnRNSync<Args extends unknown[], ReturnValue>(
fun: (...args: Args) => ReturnValue,
...args: Args
): ReturnValue;

Arguments​

fun​

A function you want to execute on the RN Runtime. It can be a function defined on the RN Runtime, a worklet or a host function. The same functions as in scheduleOnRN are accepted.

args​

Arguments to pass to the function. They must be convertible to a Serializable.

Returns​

runOnRNSync returns the return value of the function passed as the first argument. It can only return values that can be converted to a Serializable.

Remarks​

  • When called on the RN Runtime, runOnRNSync calls the function directly.
  • On the UI Runtime and Worker Runtimes, runOnRNSync works only with the Bundle Mode.
  • An error thrown by the function is rethrown on the calling runtime with the same message. In development builds its stack holds the RN Runtime frames, labeled [RN], followed by the stack of the runOnRNSync call.
  • The calling thread is blocked while the JavaScript thread runs the function. Don't call runOnRNSync in code that runs every frame.
  • runOnRNSync deadlocks if the JavaScript thread is waiting for the calling runtime at the same time. For example, a worklet on the UI Runtime must not call runOnRNSync while the RN Runtime waits in runOnUISync for the UI Runtime. Calling runOnRNSync from inside runOnUISync is safe, because that worklet already runs on the JavaScript thread.

Call table​

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

What does it mean?

Platform compatibility​

NativeWeb
✅✅