function delay(fn, delay = 0, ...args) {
  const timer = setTimeout(fn, delay, ...args);

  /**
   *
   */
  function cancel() {
    clearTimeout(timer);
  }

  /**
   * Execute the function early.
   * @returns {*} - The result of the function.
   */
  function earlyCall() {
    cancel();
    return fn(...args);
  }

  earlyCall.timer = timer;
  earlyCall.cancel = cancel;

  return earlyCall;
}
Delays function execution with possibility to execute function synchronously or cancel it.
@param {Function} fn - Function to delay
@param {number} delay - Delay time
@param {...any} args - Arguments to pass to the function
@returns {*} - Function to cancel the delay