Skip to content

Function: before()

ts
function before<TArgs, TResult>(n, fn): (...args) => TResult | undefined;

Creates a function that invokes fn at most n - 1 times. 创建最多执行 n - 1 次的包装函数。

Type Parameters

Type ParameterDescription
TArgs extends unknown[]Argument tuple / 参数元组
TResultReturn type / 返回类型

Parameters

ParameterTypeDescription
nnumberMaximum invocations before no-op / 最大调用次数(第 n 次起不再执行)
fn(...args) => TResultTarget function / 目标函数

Returns

Wrapped function / 包装函数

(...args) => TResult | undefined

Example

ts
const log = before(3, (x) => x)
log(1) // => 1
log(2) // => 2
log(3) // => undefined

Since

1.2.0

Released under the MIT License.