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

Option to resolve only last returned Promise #8

Closed
bongofury opened this issue Jun 5, 2020 · 4 comments
Closed

Option to resolve only last returned Promise #8

bongofury opened this issue Jun 5, 2020 · 4 comments

Comments

@bongofury
Copy link

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.

@sindresorhus
Copy link
Owner

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.

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.

@bongofury
Copy link
Author

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).
What I'm suggesting is to add an option to resolve the Promise returned by debouncedFn, only when an effective call to expensiveCall is performed, reject with a specific err value otherwise.

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

@fregante
Copy link
Contributor

I think the confusing part here is that regular debouncers ignore the initial calls, whereas p-debounce resolves all of them.

Unless you're specifically looking to reject the initial calls, you're better off using debounce-fn so that you get:

//=> undefined
//=> undefined
//=> Promise<3>

However you'd have to use await instead of .then since they don't return a Promise

@bongofury
Copy link
Author

Got it ;)

For this kind of things (debounce DOM events' callbacks), you should use debounce-fn .

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

No branches or pull requests

3 participants