Back to all solutions

#2666 - Allow One Function Call

Problem Description

Given a function fn, return a new function that is identical to the original function except that it ensures fn is called at most once.

  • The first time the returned function is called, it should return the same result as fn.
  • Every subsequent time it is called, it should return undefined.

Solution

/**
 * @param {Function} fn
 * @return {Function}
 */
var once = function(fn) {
  return (...args) => fn && [fn(...args), fn = undefined][0];
};