---
title: "LeetCode #2666: Allow One Function"
url: https://perrotta.dev/2026/01/leetcode-%232666-allow-one-function/
last_updated: 2026-01-06
---


[LeetCode #2666: Allow One Function](https://leetcode.com/problems/allow-one-function):

Closure:

```typescript
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
type OnceFn = (...args: JSONValue[]) => JSONValue | undefined

function once(fn: Function): OnceFn {
    let called = false;

    return function (...args) {
        if (!called) {
            called = true;
            return fn(...args);
        }
    };
}

/**
 * let fn = (a,b,c) => (a + b + c)
 * let onceFn = once(fn)
 *
 * onceFn(1,2,3); // 6
 * onceFn(2,3,6); // returns undefined without calling fn
 */
```

