@lynx-js/react / runOnMainThread

Function: runOnMainThread()

function runOnMainThread<R, Fn>(fn: Fn): (...args: Parameters<Fn>) => Promise<R>

runOnMainThread allows triggering main thread functions on the main thread asynchronously.

Type Parameters

Type Parameter
R
Fn extends (...args: any[]) => R

Parameters

ParameterTypeDescription
fnFnThe main thread functions to be called.

Returns

Function

A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread. This function returns a promise that resolves to the return value of the main thread function.

Parameters

ParameterType
...argsParameters<Fn>

Returns

Promise<R>

Example

import { runOnMainThread } from '@lynx-js/react';

async function someFunction() {
  const fn = runOnMainThread(() => {
    'main thread';
    return 'hello';
  });
  const result = await fn();
}

Defined in

@lynx-js/react/runtime/lib/worklet/call/runOnMainThread.d.ts:19

Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.