4.0.0 • Published 4 years ago

p-debounce v4.0.0

Weekly downloads
27,574
License
MIT
Repository
github
Last release
4 years ago

p-debounce

Debounce promise-returning & async functions

Install

$ npm install p-debounce

Usage

import pDebounce from 'p-debounce';

const expensiveCall = async input => input;

const debouncedFn = pDebounce(expensiveCall, 200);

for (const number of [1, 2, 3]) {
	console.log(await debouncedFn(number));
}
//=> 3
//=> 3
//=> 3

API

pDebounce(fn, wait, options?)

Returns a function that delays calling fn until after wait milliseconds have elapsed since the last time it was called.

fn

Type: Function

Promise-returning/async function to debounce.

wait

Type: number

Milliseconds to wait before calling fn.

options

Type: object

before

Type: boolean\ Default: false

Call the fn on the leading edge of the timeout. Meaning immediately, instead of waiting for wait milliseconds.

pDebounce.promise(function_)

Execute function_ unless a previous call is still pending, in which case, return the pending promise. Useful, for example, to avoid processing extra button clicks if the previous one is not complete.

function_

Type: Function

Promise-returning/async function to debounce.

import {setTimeout as delay} from 'timers/promises';
import pDebounce from 'p-debounce';

const expensiveCall = async value => {
	await delay(200);
	return value;
}

const debouncedFn = pDebounce.promise(expensiveCall);

for (const number of [1, 2, 3]) {
	console.log(await debouncedFn(number));
}
//=> 1
//=> 2
//=> 3

Related

  • p-throttle - Throttle promise-returning & async functions
  • p-limit - Run multiple promise-returning & async functions with limited concurrency
  • p-memoize - Memoize promise-returning & async functions
  • debounce-fn - Debounce a function
  • More…
@everything-registry/sub-chunk-2396test1dwebtextchecker-elementvue-bucket-flow-graphvue-chimerakahnkerygmahixilsp-mode-tsserverp-suitep-thunkp-funp-promise-utilspromise-funpromised-gatewayr3shaper-reactr3shaper-vuenext-routes-dirnolwalletreact-native-async-storereact-native-async-image-storereflowioslate-transcript-editorsnowdevsindresorhus.jsts-langservertypescript-language-server-forktypescript-language-server-rctypescript-language-server-with-ts@bedrock/web-account@block65/demonade@block65/memoized@chatfall/client@citizendev/bubble-multi-select@chatjet-ai/react@chanceseed/shoelace-ui@abdeeel/react-yo-package@devpodio/debug@dhub/cli@foxyimg/toaster@foxyimg/vue-ui@hixt/hixi@hixt/nitro@hydrofoil/shaperone-wc-shoelace@igloo-be/core-cms@gedit/preference@gqty/react@elastic/typescript-language-server@infinitebrahmanuniverse/nolb-p-@likec4/language-server@markprompt/react@nbook/typescript-language-server@nick.heiner/jscodemod@mattlyons/typescript-language-server@mersenneab/builder-plugin-dom@mersenneab/builder-plugin-search-input@ivinokur/core@joshoy/typescript-language-server@hyperspace/cli@kathondvla/sri-client@keycloak/keycloak-admin-ui@privyid/gitenak@sidwebworks/async-utils@systemfsoftware/trigger.dev@sodalife/share.js@spartez/vue-fluent@sourcegraph/typescript-language-server@tartjs/core@comet/admin-cms@comet/cms-admin@currents/playwright@devdocsai/react@callstack/byorg-slack@bluecadet/launchpad-monitor@rschristian/twind-cli@theia/preferences@theia/debug@theia/core@theia/task@theia/scm@theia/vsx-registryalfred-weigh@wbgneto/currents-playwright@xrpl/ai-react@w3block/pixchain-react-metamask@wnfs-wg/nest@villagekit/product-kit@viostream/slate-transcript-editor@uci/utils@twind/cli@types/p-debounceconnect-depscontainer-registry-syncdatoms-cliecforce-theme-clibedrock-web-accountbedrock-setupbbgobraincap-org-transcript-editorbb-transcript-editor
4.0.0

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.0

8 years ago