> 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: runOnBackground()

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

`runOnBackground` 用于在后台线程上异步触发 JS 函数。

## Type Parameters

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

## Parameters

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `f`       | `Fn` | 要调用的 JS 函数。 |

## Returns

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

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

## Example

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

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