runOnUIAsync
runOnUIAsync lets you asynchronously run workletized functions on the UI thread.
It returns a Promise of the worklet's return value. The Promise is resolved asynchronously, not immediately after the callback execution.
Reference
import { runOnUIAsync } from 'react-native-worklets';
// RN Runtime, JS thread
const result: Promise<number> = runOnUIAsync((): number => {
return 1;
});
await result; // 1


Type definitions
function runOnUIAsync<Args extends unknown[], ReturnValue>(
worklet: (...args: Args) => ReturnValue,
...args: Args
): Promise<ReturnValue>;
Arguments
worklet
A reference to a function you want to execute on the UI thread. Arguments to your function have to be passed to the function returned from runOnUIAsync i.e. runOnUIAsync(setValue, 10);.
Remarks
-
It's a common mistake to execute function inside of
runOnUIAsynclike this:. Here, the correct usage would berunOnUIAsync(myWorklet(10))runOnUIAsync(myWorklet, 10). -
The callback passed as the argument is automatically workletized and ready to be run on the UI thread.
-
Make sure not to execute
runOnUIAsyncon the UI thread as this will result in an error.