Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single call option? #3

Closed
sprat opened this issue Dec 7, 2017 · 4 comments
Closed

Single call option? #3

sprat opened this issue Dec 7, 2017 · 4 comments

Comments

@sprat
Copy link

sprat commented Dec 7, 2017

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

@sheerun
Copy link

sheerun commented Mar 6, 2019

Same problem, I'd gladly hear some recommendation. In particular I'd like debounced promise where any new promise issue cause all running promises to be cancelled and chain is replaced by last issued promise. So something like this:

const expensiveCall = input => new Promise((resolve) => setTimeout(resolve, 1000))

const debouncedFn = pDebounce(expensiveCall, 200, { leading: true });

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

@sindresorhus
Copy link
Owner

I agree, this could be useful. PR welcome if someone can come up with a good option name and behavior.

@fregante
Copy link
Contributor

all running promises to be cancelled

Promises can't be cancelled. debouncedFn(1) will still return a promise that needs to be in of 3 states:

  • pending
  • resolved
  • rejected

@sindresorhus
Copy link
Owner

AbortController could potentially be supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants