Search code examples
javascriptreactjsreduxreact-reduxredux-saga

How to use Redux Saga debounce but fetch first instead of waiting for n milliseconds


Is there a way in redux-saga to only fetch one time during for example in 5 seconds?

I know that there's debounce function:

yield debounce(5000, 'SAMPLE_ACTION', actionToFetchFirst)

but what I want is for it to fetch first rather than waiting for 5 seconds for initial fetch


Solution

  • I want is for it to fetch first rather than waiting for 5 seconds for initial fetch

    You can specify leading=true in lodash.debounce options:

    lodash.debounce(func, [wait=0], [options={}])
    

    [options.leading=false] (boolean): Specify invoking on the leading edge of the timeout.

    lodash.debounce(5000, 'SAMPLE_ACTION', { leading: true });
    

    Or just add a condition, something like:

    if (input.length > 1) fetchDebounced();
    else fetch();