debounce-promise
Advanced tools
Comparing version 3.0.2 to 3.1.0
@@ -28,5 +28,5 @@ 'use strict'; | ||
if (isCold && options.leading) { | ||
return options.accumulate ? fn.call(this, [args]).then(function (result) { | ||
return options.accumulate ? Promise.resolve(fn.call(this, [args])).then(function (result) { | ||
return result[0]; | ||
}) : fn.call.apply(fn, [this].concat(args)); | ||
}) : Promise.resolve(fn.call.apply(fn, [this].concat(args))); | ||
} | ||
@@ -62,16 +62,5 @@ | ||
clearTimeout(timer); | ||
if (options.accumulate) { | ||
fn.call(this, pendingArgs).then(function (res) { | ||
return thisDeferred.resolve(res); | ||
}, function (err) { | ||
return thisDeferred.reject(err); | ||
}); | ||
} else { | ||
fn.apply(this, pendingArgs[pendingArgs.length - 1]).then(function (res) { | ||
return thisDeferred.resolve(res); | ||
}, function (err) { | ||
return thisDeferred.reject(err); | ||
}); | ||
} | ||
Promise.resolve(options.accumulate ? fn.call(this, pendingArgs) : fn.apply(this, pendingArgs[pendingArgs.length - 1])).then(thisDeferred.resolve, thisDeferred.reject); | ||
pendingArgs = []; | ||
@@ -78,0 +67,0 @@ deferred = null; |
18
index.js
@@ -17,3 +17,5 @@ /* global setTimeout, clearTimeout */ | ||
if (isCold && options.leading) { | ||
return options.accumulate ? fn.call(this, [args]).then(result => result[0]) : fn.call(this, ...args) | ||
return options.accumulate | ||
? Promise.resolve(fn.call(this, [args])).then(result => result[0]) | ||
: Promise.resolve(fn.call(this, ...args)) | ||
} | ||
@@ -41,10 +43,10 @@ | ||
clearTimeout(timer) | ||
if (options.accumulate) { | ||
fn.call(this, pendingArgs) | ||
.then(res => thisDeferred.resolve(res), err => thisDeferred.reject(err)) | ||
} else { | ||
fn.apply(this, pendingArgs[pendingArgs.length - 1]) | ||
.then(res => thisDeferred.resolve(res), err => thisDeferred.reject(err)) | ||
} | ||
Promise.resolve( | ||
options.accumulate | ||
? fn.call(this, pendingArgs) | ||
: fn.apply(this, pendingArgs[pendingArgs.length - 1]) | ||
) | ||
.then(thisDeferred.resolve, thisDeferred.reject) | ||
pendingArgs = [] | ||
@@ -51,0 +53,0 @@ deferred = null |
{ | ||
"name": "debounce-promise", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"description": "Create a debounced version of a promise returning function", | ||
@@ -5,0 +5,0 @@ "main": "dist/index", |
@@ -125,2 +125,13 @@ /* global setTimeout */ | ||
test('Converts the return value from the producer function to a promise', async t => { | ||
let callCount = 0 | ||
const debounced = debounce(() => ++callCount, 10) | ||
debounced() | ||
debounced() | ||
await debounced() | ||
t.equal(callCount, 1) | ||
}) | ||
test('calls debounced function and accumulates arguments', async t => { | ||
@@ -170,1 +181,27 @@ function squareBatch (args) { | ||
}) | ||
test('accumulate works with non-promise return value', async t => { | ||
let callNo = 1 | ||
function squareBatch (args) { | ||
if (callNo === 1) { | ||
t.deepEqual(args, [[1]]) | ||
} | ||
if (callNo === 2) { | ||
t.deepEqual(args, [[2], [3]]) | ||
} | ||
callNo++ | ||
return args.map(arg => arg * arg) | ||
} | ||
const square = debounce(squareBatch, 10, {leading: true, accumulate: true}) | ||
const one = square(1) | ||
const two = square(2) | ||
const three = square(3) | ||
await sleep(20) | ||
t.equal(await one, 1) | ||
t.equal(await two, 4) | ||
t.equal(await three, 9) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18170
314