Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 417 Bytes

once-function.md

File metadata and controls

23 lines (21 loc) · 417 Bytes
title description author tags
Once Function
Ensures a function is only called once.
axorax
function,once
const once = (func) => {
  let called = false;
  return (...args) => {
    if (!called) {
      called = true;
      return func(...args);
    }
  };
};

// Usage:
const initialize = once(() => console.log('Initialized!'));
initialize(); // Output: Initialized!
initialize(); // No output