> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# Function: runOnMainThread()

> **runOnMainThread**\<`R`, `Fn` _extends_ (...`args`: `any`\[]) => `R`>(`fn`: `Fn`): (...`args`: `Parameters`\<`Fn`>) => `Promise`\<`R`>

`runOnMainThread` 用于在主线程上异步触发主线程函数。

## Type Parameters

| Type Parameter                              |
| ------------------------------------------- |
| `R`                                         |
| `Fn` _extends_ (...`args`: `any`\[]) => `R` |

## Parameters

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `fn`      | `Fn` | 要调用的主线程函数。  |

## Returns

一个函数。以要传给该主线程函数的参数调用它，即可在主线程上触发该主线程函数。该函数返回一个 Promise，其 resolve 值为该主线程函数的返回值。

(...`args`: `Parameters`\<`Fn`>) => `Promise`\<`R`>

## Example

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

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