Skip to content

Create a debounced version of a promise returning function

Notifications You must be signed in to change notification settings

Newstex/debounce-promise

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

debounce-promise Build Status

NPM

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(function(num) {
  return saveCycles('call no #' + num).then(function(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(function(num) {
  return saveCycles('call no #' + num).then(function(value) {
    console.log(value)
  })
})

//=> call no #1
//=> call no #1
//=> call no #1
//=> call no #1

Api

debounce(func, [wait=0], [{leading: 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.

Supports passing a function as the wait parameter, which provides a way to lazily or dynamically define a wait timeout.

About

Create a debounced version of a promise returning function

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%