DEV Community

Discussion on: The most elegant debounce code you'll ever write featuring XState

Collapse
codingdive profile image
Mikey Stengel Author

Hello beautiful people!

Based on some feedback, I have written an improved version that debounces with state transitions. The advantage is that it cancels existing requests (by ignoring their result) and in XState version 5, AbortController support will be built in, meaning we get to actually cancel the promise natively! You can find the improved CodeSandbox here
codesandbox.io/s/xstate-transition...

The way debouncing is handled in this blog post, is still my preferred way when in need to debounce synchronous code. E.g when you have a really big array and need to do some computation on it, delayed events are the way to go. For async logic, I now recommend you to follow the Codesandbox and just use transitions.

I'm hoping to update the blog post too at some point.