Side Effects with throttle
In the previous example, the validateUsername
function is called whenever the username input changes on each user typing. This will end up with an unnecessary amount of remote requests. If you don't want to flood your server, you probably want to throttle the requests.
We can achieve this by running a throttle
effect on setUsername
mutation which only calls the validateUsername
function with a max frequency, for example, one request per second.
Instead of configuring a side effect with a function, we can configure it as an object which has keys throttle
and ms
where the throttle
will be only be run max one time in 1000 ms, any setUsername
mutations happening during the 1000 ms will be ignored if there was already a throttle
effect running.
See the full example here
Last updated