3.4 Выполнение гонки между несколькими эффектами
Sometimes we start multiple tasks in parallel but we don't want to wait for all of them, we just need to get the winner: the first one that resolves (or rejects). The race
Effect offers a way of triggering a race between multiple Effects.
The following sample shows a task that triggers a remote fetch request, and constrains the response within a 1 second timeout.
Another useful feature of race
is that it automatically cancels the loser Effects. For example, suppose we have 2 UI buttons:
The first starts a task in the background that runs in an endless loop
while (true)
(e.g. syncing some data with the server each x seconds).Once the background task is started, we enable a second button which will cancel the task
In the case a CANCEL_TASK
action is dispatched, the race
Effect will automatically cancel backgroundTask
by throwing a cancellation error inside it.
Last updated