FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Once

10mins

Implement a function once(fn) that accepts a callback fn and restricts its invocation to at most once.

The function should:

  • Return a new function that calls fn only on the first invocation.
  • On subsequent calls, return the result from the first call without invoking fn again.

Examples

const initialize = once(() => "initialized"); initialize(); // "initialized" initialize(); // "initialized" (same result, fn not called again)

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests