Closures & Scope

Once Wrappers

Once Wrappers

A once wrapper is a closure that controls whether another function is allowed to run. It stores two private facts: whether the wrapped function has already run, and what result it produced.

This pattern is useful for initialization code, analytics events, and setup work that must not repeat. The wrapper should keep returning the first result instead of running the original function again with later arguments.

Your task: Complete once. It receives a function and returns a new function. The returned function should call the original function only the first time, remember that result, and return the remembered result on every later call.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Once Wrappers

A once wrapper is a closure that controls whether another function is allowed to run. It stores two private facts: whether the wrapped function has already run, and what result it produced.

This pattern is useful for initialization code, analytics events, and setup work that must not repeat. The wrapper should keep returning the first result instead of running the original function again with later arguments.

Your task: Complete once. It receives a function and returns a new function. The returned function should call the original function only the first time, remember that result, and return the remembered result on every later call.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Console

Run code to see output.