Once

Create a function that can only be called once, returning the cached result for subsequent invocations.

Code

Boilerplates
const once = (fn) => {
  let called = false, result;
  return (...args) => {
    if (!called) {
      called = true;
      result = fn(...args);
    }
    return result;
  };
};

let count = 0;
const increment = once(() => ++count);
return [increment(), increment(), increment()].join(',');
Browser·fetch() may be limited by CORS

More JavaScript Snippets