For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/function/before.md.

before es-toolkit

Creates a new function that limits the number of times a function can be called.

const limitedFunc = before(n, func);

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.

import { before } from 'es-toolkit/function';

const beforeFn = before(3, () => {
  console.log('executed');
});

// Logs 'executed'
beforeFn();

// Logs 'executed'
beforeFn();

// Logs nothing
beforeFn();

// Logs nothing
beforeFn();

This is useful for tasks that should only be executed once, such as initialization or setup.

let initialized = false;

const initialize = before(2, () => {
  console.log('Initializing...');
  initialized = true;
});

// Logs 'Initializing...' and performs initialization
initialize();

// Does nothing as it's already initialized
initialize();

Parameters

nnumberrequired

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.

funcFrequired

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.

(...args: Parameters<F>) => ReturnType<F> | undefined

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.