before es-toolkit
Creates a new function that limits the number of times a function can be called.
Usage
before(n, func)
Use before when you want to limit a function to be executed only up to a specific number of times. The function will execute until the n-1-th call, and from the n-th call onwards, it will no longer execute.
This is useful for tasks that should only be executed once, such as initialization or setup.
Parameters
The maximum number of times the returned function can call func. If n is 0, func will not be called. If it's a positive integer, it will be called up to n-1 times.
The function whose invocation count will be limited.
Returns
A new function that tracks the number of calls and executes func only up to the n-1-th call. Returns undefined for calls from the n-th onwards.
Throws
Throws an error when n is not an integer or is negative.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.