debounce-promise
Advanced tools
Comparing version 1.1.1 to 2.0.0
@@ -28,5 +28,7 @@ /* global setTimeout, clearTimeout */ | ||
nextArgs = args; | ||
var onTimeout = run.bind(this, nextArgs, resolve, reject); | ||
if (!pending) { | ||
if (leading) { | ||
pending = fn.apply(this, nextArgs); | ||
onTimeout = clear; | ||
} else { | ||
@@ -40,3 +42,3 @@ pending = new Promise(function (_resolve, _reject) { | ||
clearTimeout(timer); | ||
timer = setTimeout(run.bind(this, nextArgs, resolve, reject), getWait(wait)); | ||
timer = setTimeout(onTimeout, getWait(wait)); | ||
return pending; | ||
@@ -43,0 +45,0 @@ }; |
@@ -10,5 +10,7 @@ /* global setTimeout, clearTimeout */ | ||
nextArgs = args | ||
let onTimeout = run.bind(this, nextArgs, resolve, reject) | ||
if (!pending) { | ||
if (leading) { | ||
pending = fn.apply(this, nextArgs) | ||
onTimeout = clear | ||
} else { | ||
@@ -22,3 +24,3 @@ pending = new Promise((_resolve, _reject) => { | ||
clearTimeout(timer) | ||
timer = setTimeout(run.bind(this, nextArgs, resolve, reject), getWait(wait)) | ||
timer = setTimeout(onTimeout, getWait(wait)) | ||
return pending | ||
@@ -25,0 +27,0 @@ } |
{ | ||
"name": "debounce-promise", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Create a debounced version of a promise returning function", | ||
@@ -5,0 +5,0 @@ "main": "dist/index", |
@@ -31,2 +31,18 @@ /* global setTimeout */ | ||
test('does not call the given function again after the timeout when leading=true if executed only once', async t => { | ||
let callCount = 0 | ||
const debounced = debounce(async () => callCount++, 100, {leading: true}) | ||
await debounced() | ||
await sleep(200) | ||
t.equal(callCount, 1) | ||
}) | ||
test('calls the given function again after the timeout when leading=true if executed multiple times', async t => { | ||
let callCount = 0 | ||
const debounced = debounce(async () => callCount++, 100, {leading: true}) | ||
await* [1, 2, 3, 4].map(debounced) | ||
await sleep(200) | ||
t.equal(callCount, 2) | ||
}) | ||
test('waits until the wait time has passed', async t => { | ||
@@ -33,0 +49,0 @@ let callCount = 0 |
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
11093
188