Function: once()
ts
function once<TArgs, TResult>(fn): (...args) => TResult;Creates a function that can only run once. 创建一个只能执行一次的函数。
Type Parameters
| Type Parameter | Description |
|---|---|
TArgs extends unknown[] | The argument tuple / 参数元组类型 |
TResult | The return type / 返回值类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (...args) => TResult | The target function / 目标函数 |
Returns
A function that reuses the first result / 重复返回首次结果的函数
(...args) => TResult
Example
ts
const init = once(() => 1)
init()
// => 1Since
1.2.0