如何使用 Redux Saga debounce 但首先获取而不是等待 n 毫秒

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

redux-saga 中有没有办法在例如 5 秒内只获取一次?

我知道有 debounce 功能:

yield debounce(5000, 'SAMPLE_ACTION', actionToFetchFirst)

但我想要的是先获取它,而不是等待 5 秒进行初始获取

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

您可以在 lodash.debounce options 中指定 leading=true:

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

或者只添加一个条件,例如:

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

如果我理解正确的话,你正在寻找的东西叫做 throttle 意思是在 5 秒的时间跨度内这个动作只会被调用一次,如果是这样你可以使用 lodash throttle func 为此 https://lodash.com/docs/4.17.15#throttle