Giter VIP home page Giter VIP logo

p-debounce's Introduction

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]) {
	(async () => {
		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]) {
	(async () => {
		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…

p-debounce's People

Contributors

bendingbender avatar darcyparker avatar fregante avatar matamatanot avatar rafael09ed avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

p-debounce's Issues

Call immediately flag

Having an option to call the function immediately could be useful but it would require changing the API because currently all parameters are being passed into fn. There would be a bit of weirdness with creating value for multi parameter functions so feedback on how to make that work would be appreciated. I don't have a specific use case for this but I saw it while doing research. Could be useful in sindresorhus/debounce-fn as well but faces same problem.

Here is what I was thinking:

pDebounce(fn, wait, options?)

.....

pDebounce()(value, immediate?)

value

Type: any

The value to be passed to fn, the function being debounced.

immediate

Type: boolean
Default: false

If false, debounce will follow normal behavior. If true the debounced function will be called immediately.

Feature Request: CommonJS Support (Drop ESM / Babel Requirement)

Hello!

I'm a huge fan of your work and use got everywhere I go.

Use case: I don't transpile my code or use typescript; I make use require-in-the-middle and cannot easily switch my type in package.json to module nor change all of my require to import.

I'm not seeing a way to use this module in Node 14 unless my project is 100% ESM and/or using Babel.

Am I missing something?

Thanks for your great work!

Option to resolve only last returned Promise

I created something very similar to this package, the only difference is that, when the async function is not called due to debounce timer, the related Promise is rejected with a default object value.

That was done to be used in scenarios like the following.
I have an input element, whose 'change' event is bound to a callback that makes an API call and, then, updates the DOM. This callback needs to be debounced to prevent overloading the server with multiple calls.

// pseudocode
inputEl.addEventListener('change', (e) => {
   callApiDebounced(e.target.value).then(updateDOM)
})

With the actual implementation of p-debounce, multiple calls to the debounced functions would result in multiple updates to the DOM (with the same content). This could be prevented by resolving only the last Promise returned.

I'm wondering if this use case could be useful for other people.

Usage example is wrong

I was not sure if the example was wrong or if I was just dumb:

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));
}

outputs:

1
2
3

README says:

3
3
3

Also I'm not sure this example demonstrates the value of the lib. Calling expensiveCall alone in the for loop would produce the same output.

Bad example in README

There is this example in README (link):

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));
}
//=> 3
//=> 3
//=> 3

Actual output:

1
2
3

Single call option?

I would like to debounce a function returning a promise but also limit the concurrent calls of my function to 1. That is, if a previous invocation is still running after the debounce delay is reached, delay again and again... until the last call finish and the debounce delay is expired (no other calls occured during the waiting time).

I know you also developed another module to limit conccurency, but I don't think I can combine it with this one to obtain the behavior I need. In my use case (an async database update), it's important that the last call occur ;)

Handling exceptions?

I am wondering if exception handling could be added to this library? And if so, what the policy should be about rejecting the waiting promises? If all of them are rejected, then it will look like the error happened many times?

ES6 syntax used in module

This source code uses arrow functions (and possibly other ES6 features, I didn't look too closely), which means it is not compatible with browser environments where IE11 support is required.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.