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

Adding trigger function. #41

Merged
merged 3 commits into from
May 25, 2024
Merged

Adding trigger function. #41

merged 3 commits into from
May 25, 2024

Conversation

olafurw
Copy link
Contributor

@olafurw olafurw commented May 23, 2024

I'm using the library and it's going great so far. I'm using it to debounce relatively expensive API calls but there are instances where it's likely that a call is already "queued" (not a guarantee) but the user performed an action where I want to call it immediately and clear the underlying call.

And I don't want to call the underlying function, because if there is a queued debounce then I want to avoid calling the API twice, so what I end up doing is something like.

fn();
fn.flush();

So my suggestion is a trigger function (you can rename it to whatever you want, trigger, invoke, run, etc) where it calls the underlying function and clears any queued timers. So then in those instances I can just do fn.trigger()

index.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

Makes sense. This needs docs addition in readme and index.d.ts

@olafurw
Copy link
Contributor Author

olafurw commented May 24, 2024

Doc things added. Did some refactoring also, hope it's ok.
Now the main functions are kinda boring since they just call each other or call run()

@sindresorhus sindresorhus merged commit 1fe9160 into sindresorhus:main May 25, 2024
2 checks passed
@sindresorhus
Copy link
Owner

Thanks :)

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

Successfully merging this pull request may close these issues.

2 participants