Debounce function implemented with promises : <p>I'm trying to implement a debounce function that works with a promise in javascript. That way, each caller can consume the result of the "debounced" function using a Promise. Here is the best I have been able to come up with so far:</p>

<pre><code>function debounce(inner, ms = 0) {
  let timer = null;
  let promise = null;
  const events = new EventEmitter();  // do I really need this?

  return function (...args) {
    if (timer == null) {
      promise = new Promise(resolve =&gt; {
        events.once('done', resolve);
      });
    } else {
      clearTimeout(timer);
    }

    timer = setTimeout(() =&gt; {
      events.emit('done', inner(...args));
      timer = null;
    }, ms);

    return promise;
  };
}
</code></pre>

<p>Ideally, I would like to implement this utility function <em>without</em> introducing a dependency on EventEmitter (or implementing my own basic version of EventEmitter), but I can't think of a way to do it. Any thoughts?</p>
