How to get last value from throttled function
<p>The documentation for the <a href="https://lodash.com/docs#throttle" rel="nofollow">_.throttle</a> function states that:</p>

<blockquote>
  <p>Creates a throttled function that only invokes func at most once per
  every wait milliseconds. The throttled function comes with a cancel
  method to cancel delayed func invocations and a flush method to
  immediately invoke them. Provide an options object to indicate whether
  func should be invoked on the leading and/or trailing edge of the wait
  timeout. The func is invoked with the last arguments provided to the
  throttled function. Subsequent calls to the throttled function return
  the result of the last func invocation</p>
</blockquote>

<p>I'm interested in this line:</p>

<blockquote>
  <p>Subsequent calls to the throttled function return
  the result of the last func invocation</p>
</blockquote>

<p>I've tried:</p>

<pre><code>var throttled = _.throttle(updateModelData, 1000);
service.on('change', function () {
    throttled(5);
});

function updateModelData(data) {
    // all calls here log 5's
    console.log(data);
    return data;
}

setTimeout(function() {
    throttled(); // here updateModelData is executed with `undefined` value
}, 5000);
</code></pre>

<p>The problem is that <code>throttled()</code> triggers function without returning the data. How can I invoke it so that it returns last data?</p>

<p>EDIT:</p>

<p>According to source code, the value will be returned only if no pending function call exists <code>isCalled === false</code>:</p>

<pre><code>  function debounced() {
    args = arguments;
    stamp = now();
    thisArg = this;
    trailingCall = trailing &amp;&amp; (timeoutId || !leading);

    if (maxWait === false) {
      var leadingCall = leading &amp;&amp; !timeoutId;
    } else {
      if (!maxTimeoutId &amp;&amp; !leading) {
        lastCalled = stamp;
      }
      var remaining = maxWait - (stamp - lastCalled),
          isCalled = remaining &lt;= 0 || remaining &gt; maxWait;

      !!!!! HERE
      if (isCalled) {
        if (maxTimeoutId) {
          maxTimeoutId = clearTimeout(maxTimeoutId);
        }
        lastCalled = stamp;
        result = func.apply(thisArg, args);
      }
      else if (!maxTimeoutId) {
        maxTimeoutId = setTimeout(maxDelayed, remaining);
      }
    }
    ...
    return result;
  }
</code></pre>

<p>So the following will work:</p>

<pre><code>var throttled = _.throttle(updateModelData, 10000);
service.on('change', function () {
    throttled(5);
});

function updateModelData(data) {
    // all calls here log 5's
    console.log(data);
    return data;
}

setTimeout(function() {
    throttled(); // returns 5
}, 15000);
</code></pre>