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,
runOnRNSynccalls the function directly. - On the UI Runtime and Worker Runtimes,
runOnRNSyncworks 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 therunOnRNSynccall. - The calling thread is blocked while the JavaScript thread runs the function. Don't call
runOnRNSyncin code that runs every frame. runOnRNSyncdeadlocks if the JavaScript thread is waiting for the calling runtime at the same time. For example, a worklet on the UI Runtime must not callrunOnRNSyncwhile the RN Runtime waits inrunOnUISyncfor the UI Runtime. CallingrunOnRNSyncfrom insiderunOnUISyncis safe, because that worklet already runs on the JavaScript thread.
Call table
| Mode | RN Runtime | UI Runtime | Worker Runtime |
|---|---|---|---|
| Bundle Mode | ✅ | ✅ | ✅ |
| Legacy Eval Mode | ✅ | ❌ | ❌ |
Platform compatibility
| Native | Web |
|---|---|
| ✅ | ✅ |