after
Creates a function that only executes starting from the n-th call. The provided function will be invoked starting from the n-th call.
The is particularly useful for scenarios involving events or asynchronous operations where an action should occur only after a certain number of invocations.
Signature
typescript
function after<F extends (...args: any[]) => any>(
n: number,
func: F
): (...args: Parameters<F>) => ReturnType<F> | undefined;Parameters
n(number): The number of calls required forfuncto execute.func(F): The function to be invoked.
Returns
((...args: Parameters<F>) => ReturnType<F> | undefined): A new function that:
- Tracks the number of calls.
- Invokes
funcstarting from then-th call. - Returns
undefinedif fewer thanncalls have been made.
Throws
Throws an error if n is negative.
Examples
typescript
import { after } from 'es-toolkit/function';
const mockFn = () => {
console.log('called');
};
const afterFn = after(3, mockFn);
// Will not log anything.
afterFn();
// Will not log anything.
afterFn();
// Will log 'called'.
afterFn();
