Package Exports
- debounce-promise
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (debounce-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
debounce-promise
Create a debounced version of a promise returning function
Install
npm i -S debounce-promise
Usage example
var debounce = require('debounce-promise')
function expensiveOperation(value) {
return Promise.resolve(value)
}
var saveCycles = debounce(expensiveOperation, 100);
[1, 2, 3, 4].forEach(num => {
return saveCycles('call no #' + num).then(value => {
console.log(value)
})
})
// Will only call expensiveOperation once with argument `4` and print:
//=> call no #4
//=> call no #4
//=> call no #4
//=> call no #4
With leading=true
var debounce = require('debounce-promise')
function expensiveOperation(value) {
return Promise.resolve(value)
}
var saveCycles = debounce(expensiveOperation, 100, {leading: true});
[1, 2, 3, 4].forEach(num => {
return saveCycles('call no #' + num).then(value => {
console.log(value)
})
})
//=> call no #1
//=> call no #1
//=> call no #1
//=> call no #1
With accumulate=true
var debounce = require('debounce-promise')
function squareValues (values) {
return Promise.all(values.map(val => val * val))
}
var square = debounce(squareValues, 100, {accumulate: true});
[1, 2, 3, 4].forEach(num => {
return square(num).then(value => {
console.log(value)
})
})
//=> 1
//=> 4
//=> 9
//=> 16
Api
debounce(func, [wait=0], [{leading: true|false, accumulate: true|false})
Returns a debounced version of func
that delays invoking until after wait
milliseconds.
Set leading: true
if you
want to call func
immediately and use the value from the first call for all subsequent promises.
Set accumulate: true
if you want the debounced function to be called with an array of all the arguments received while waiting.
Supports passing a function as the wait
parameter, which provides a way to lazily or dynamically define a wait timeout.