Once (Function Wrapper)
fn only on the first call. Cache and return the result on subsequent calls without re-invoking fn. This tests closures, memoization, and side-effect control. Concepts: higher-order, functions, closure.Arguments
fn: Function —The function to call once.
Returns
Function — fn once.Solution idea
Wrap a function with a closure that tracks whether it has been called and stores its return value. The first call executes the original function; every later call returns the cached result. This pattern is common for one-time initialization and idempotent setup. Key concepts: higher-order, functions, closure.
const addOnce = once((a, b) => a + b);
addOnce(1, 2); // 3
addOnce(5, 6); // still 3
const obj = { x: 2, get: once(function (n) { return this.x + n; }) };
obj.get(3); // 5Solution
Overview
Wrap a function with a closure that tracks whether it has been called and stores its return value. The first call executes the original function; every later call returns the cached result. This pattern is common for one-time initialization and idempotent setup. Key concepts: higher-order, functions, closure.
Approach: Closure with a called flag
Algorithm:
- Create
called = falseandresultin the outer scope. - Return a wrapper function that checks
called. - If not called, invoke
fnwith the current arguments/this, store the result, and setcalled = true. - Always return
result.
Complexity: O(1) per call, O(1) extra space.
export default function once(fn) {
let called = false;
let result;
return function (...args) {
if (!called) {
called = true;
result = fn.apply(this, args);
}
return result;
};
}
export default function once<F extends (...args: any[]) => any>(fn: F): F {
let called = false;
let result: ReturnType<F>;
return function (this: any, ...args: Parameters<F>) {
if (!called) {
called = true;
result = fn.apply(this, args);
}
return result;
} as F;
}
Notes & Pitfalls
Preserve thisby usingfn.apply(this, args).Subsequent calls return the first result, even with different arguments. If fnthrows, this implementation still marks it as called.
Functions that return undefinedstill count as called.Wrapper should work with any number of arguments.
Closures for private state. Higher-order functions.
Common mistakes on this challenge
Record the first call with a flag, not with the result
The check wraps a function that returns undefinedand calls the wrapper twice; the function must still run only once.Keep a separate calledboolean and set it the first time.Return the cached result on every later call, even when it is undefined.
Once wrapper should cache first result and stop re-invocation
The wrapped function should execute exactly once with stable return value thereafter. Store calledflag and cachedresultin closure state.Invoke underlying function only on first call and cache the output. Use fn.apply(this, args)to preserve call-site context.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.