From 102d6d8c8436b7e9e2495618e7f9f460e3c16508 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 6 Sep 2012 20:49:06 -0700 Subject: [PATCH] Capture the result of the last `func` call in `_.throttle` and `_.debounce`. Former-commit-id: 2e783fad2e86824bf098bdb24ca6911317576f32 --- lodash.js | 4 ++-- test/test.js | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lodash.js b/lodash.js index f2bfdf2ad..a70063fed 100644 --- a/lodash.js +++ b/lodash.js @@ -3488,7 +3488,7 @@ function delayed() { timeoutId = null; if (!immediate) { - func.apply(thisArg, args); + result = func.apply(thisArg, args); } } @@ -3674,7 +3674,7 @@ function trailingCall() { lastCalled = new Date; timeoutId = null; - func.apply(thisArg, args); + result = func.apply(thisArg, args); } return function() { diff --git a/test/test.js b/test/test.js index 8c20b3e9a..f8243e949 100644 --- a/test/test.js +++ b/test/test.js @@ -318,7 +318,17 @@ QUnit.module('lodash.debounce'); (function() { - test('subsequent "immediate" debounced calls should return the result of the first call', function() { + asyncTest('subsequent debounced calls return the last `func` result', function() { + var debounced = _.debounce(function(value) { return value; }, 100); + debounced('x'); + + setTimeout(function() { + equal(debounced('y'), 'x'); + QUnit.start(); + }, 220); + }); + + test('subsequent "immediate" debounced calls return the last `func` result', function() { var debounced = _.debounce(function(value) { return value; }, 100, true), result = [debounced('x'), debounced('y')];