3.1.2 • Published 5 years ago

debounce-promise v3.1.2

Weekly downloads
159,355
License
MIT
Repository
github
Last release
5 years ago

debounce-promise

Build Status Standard - JavaScript Style Guide

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(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 #4
//=> call no #4
//=> call no #4

With accumulate=true

var debounce = require('debounce-promise')

function squareValues (argTuples) {
  return Promise.all(argTuples.map(args => args[0] * args[0]))
}

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 and return its promise immediately.

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.

Example timeline illustration

function refresh() {
  return fetch('/my/api/something')
}
const debounced = debounce(refresh, 100)
time (ms) ->   0 ---  10  ---  50  ---  100 ---
-----------------------------------------------
debounced()    | --- P(1) --- P(1) --- P(1) ---
refresh()      | --------------------- P(1) ---
const debounced = debounce(refresh, 100, {leading: true})
time (ms) ->   0 ---  10  ---  50  ---  100 ---  110 ---
--------------------------------------------------------
debounced()    | --- P(1) --- P(2) --- P(2) --- P(2) ---
refresh()      | --- P(1) --------------------- P(2) ---
@xh/hoist@gooddata/sdk-ui-kit@balena/jellyfish-ui-components@commercetools-uikit/data-table-manager@andromeda-ui/selecttest-dsm-github-harmonizerpassbolt_extensionoptimade.sciencemaestro-vsc-extension-plugin@roboholix/shared-components@jgrimard/homebridge-vivint-mfa-testsub-tv@everything-registry/sub-chunk-1457flow-engine-cliformik-storefunbox_testenforce-uihomebridge-plugin-verahomebridge-polestargfh-clientharvesterjsgenesys-cloud-streaming-clientgit-add-and-commitgithub-harmonizerhomebridge-daikin-airbasehomebridge-vivinthomebridge-vivint-fixedhomebridge-vivint-latestpagelogicobojobo-repositorynukeleusnoflo-nodejsnpm-all-packagespassbolt-styleguideopengate-vjsfluxury-escapesld-qanda-ruxmg-formsmedipass-react-selectmobx-localforage-storemobx-blocksminta-studiominoshiromodokipubquiz-managerpurecloud-streaming-clienttiny-ssgtchen-vuelayersunbagreact-booking-formshapeable-starter-websairamorionlibsaurabhorionlibresilio-sync-watch-configreact-cimpress-commenttsxlogintsxlogin45vic-eligible-test@veupathdb/components@veupathdb/eda@urban-bot/core@sitecore-discover/core@sitecore-search/core@souvik1991/react-lazy-load-imagewao-react-shared-components@airbnb/lunar@zimyo/components@zeroer/general-tools@byudaniel/refreshing-config-store@cardstack/git@aics/fms-file-explorer-core@baking-bad/vjsf@balansse/homebridge-vivintasseco-commons@getflip/swirl-components@grafana/prometheusawesome-debounce-promise@chirpy-dev/main-app@chirpy-dev/ui@bazo/js-dev-scripts@zalastax/nolb-deb@pratico/web-editor@usercentric/uc-design-system@dal/dftr-app@dappworks/form@dappworks/jsonview@omega/runtime@omm/cerebro-confluence-jiravuelayersvuelayers-c@onebeyond/react-form-builder@ticketlog/americas-ui@guidesmiths/react-form-builderbiz-docs-ui-react@atlaskit/link-create@atlaskit/link-datasource@best-apps/gfx-editor@techslice-official/sandbox@bestdoctor/ke@commercetools-frontend/experimental-components@commercetools-extensions/change-history
3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

6 years ago

3.0.2

6 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.1.1

7 years ago

2.1.0

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.5

8 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago