-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Option to resolve only last returned Promise #8
Comments
What "related promise"? When the async function is not called, there is no promise. I think I'm not fully understanding what you're describing. |
Taking as example the usage code in the doc. const pDebounce = require('p-debounce');
const expensiveCall = async input => input;
const debouncedFn = pDebounce(expensiveCall, 200);
for (const i of [1, 2, 3]) {
debouncedFn(i).then(console.log);
}
//=> 3
//=> 3
//=> 3 Each time you call debouncedFn you get back a Promise (this is what I was referring to). With this logic in place the example could be rewritten as: for (const i of [1, 2, 3]) {
debouncedFn(i).then(
console.log,
(err) => {
if (err.debounced) { console.log('This call has been debounced'); return; }
throw err;
}
);
}
//=> This call has been debounced
//=> This call has been debounced
//=> 3 Don't know if this is worth to be added, I replied only to clarify, thanks ;) . |
I think the confusing part here is that regular debouncers ignore the initial calls, whereas Unless you're specifically looking to reject the initial calls, you're better off using
However you'd have to use |
Got it ;) For this kind of things (debounce DOM events' callbacks), you should use debounce-fn . |
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.
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.
The text was updated successfully, but these errors were encountered: