Skip to content

Function: once() ​

ts
function once<TArgs, TResult>(fn): (...args) => TResult;

Creates a function that can only run once. 创建一个只能执行一次的函数。

Type Parameters ​

Type ParameterDescription
TArgs extends unknown[]The argument tuple / 参数元组类型
TResultThe return type / 返回值类型

Parameters ​

ParameterTypeDescription
fn(...args) => TResultThe target function / 目标函数

Returns ​

A function that reuses the first result / 重复返回首次结果的函数

(...args) => TResult

Example ​

ts
const init = once(() => 1)
init()
// => 1

Since ​

1.2.0

Released under the MIT License.