3.1.2 • Published 6 years ago

debounce-promise v3.1.2

Weekly downloads
159,355
License
MIT
Repository
github
Last release
6 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) ---
@andromeda-ui/selecttest-dsm-github-harmonizerpassbolt_extensionoptimade.sciencemaestro-vsc-extension-plugin@roboholix/shared-components@jgrimard/homebridge-vivint-mfa-testsub-tv@everything-registry/sub-chunk-1457tchen-vuelayersvic-eligible-test@commercetools-frontend/experimental-components@comperio/discovery-elements@dal/dftr-app@commercetools-extensions/change-history@commercetools-frontend-extensions/change-history@commercetools-uikit/data-table-manager@postalk/components@paypossible/formik-store@pratico/web-editortsxlogintsxlogin45tiny-ssg@airbnb/lunar@atlaskit/link-datasource@atlaskit/link-create@aics/fms-file-explorer-core@aws-amplify/sandbox@balansse/homebridge-vivint@baking-bad/vjsf@balena/jellyfish-ui-components@bazo/js-dev-scripts@bestdoctor/ke@best-apps/gfx-editor@cardstack/git@byudaniel/refreshing-config-store@chirpy-dev/main-app@chirpy-dev/ui@dappworks/form@dappworks/jsonviewvuelayersvuelayers-cwao-react-shared-componentsunbagzerionprokovanmail@financial-times/x-topic-search@dword-design/base-plugin-babel@guidesmiths/react-form-builder@igloo-be/core-cms-cli@gooddata/sdk-ui-ext@gooddata/sdk-ui-kit@etherspot/react-transaction-buidler@getflip/swirl-components@grafana/prometheus@infinitebrahmanuniverse/nolb-deb@ks-console/shared@kotani2024/kovanmail@linzjs/step-ag-grid@lumiscaphe/viewer@michaelmade/homebridge-vivint-latest@muenchen/vjsf@mozartspa/mobx-form@nagoos/google-maps-autocomplete-vuetify@namchey/linkpreview@emuanalytics/flow-cli@interface-technologies/iti-react@j2inn/ui@kiroboio/fct-sdk@home-assistant-matter-hub/core@omega/runtime@omm/cerebro-confluence-jira@pucki/passbolt-styleguide@onebeyond/react-form-builder@souvik1991/react-lazy-load-image@sitecore-discover/core@sitecore-search/coreminoshiromodokiminta-studiomobx-localforage-storemobx-blocksmg-formsopengate-vjsfobojobo-repositorynukeleuspassbolt-styleguidepagelogicreact-booking-formreact-cimpress-commentsaurabhorionlibsairamorionlibpurecloud-streaming-clientnoflo-nodejsresilio-sync-watch-configpubquiz-managerrainbowprokovanmailshapeable-starter-webgit-add-and-commitgithub-harmonizergraphql-language-servicehomebridge-daikin-airbase
3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

7 years ago

3.0.2

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.5

9 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago