Check out a free preview of the full Advanced State Management in React (feat. Redux and MobX) course:
The "Saga Helpers" Lesson is part of the full, Advanced State Management in React (feat. Redux and MobX) course featured in this preview video. Here's what you'd learn in this lesson:

Overview of techniques for handling race conditions using Saga helpers like takeLatest and takeEvery.

Get Unlimited Access Now

Transcript from the "Saga Helpers" Lesson

>> Steve Kinney: Sagas are cool, right? Because they allow like, we can like at least abstract our complexity to one place. It kind of intercepts these things. But there's more to them, right? This is like, we could do a full thing just on sagas in this case. But we have takeEvery and takeLatest.

[00:00:21] takeEvery will look for every event, takeLatest will cancel ones that are currently in flight. So if you're, for instance, streaming a chat room, you don't wanna display the messages twice. You just wanna get the latest version of that. It will actually cancel previous versions. We can also do other things.

[00:00:41] We can take every event and do stuff with it, right. So in this case, we are again logging the actions from the state by listening to every single event that comes through. We can also create, and this is where, what I think is really cool about is that we're not gonna get a chance to go fully into, is that you have a whole lot of really interesting concurrency primitives.

[00:01:00] Right, so when we fire off a promise normally. We don't really have the ability to cancel that promise. So a thunk, you thought you dispatched it. It's going, you have that then and catch. You can't say actually stop. Right, so this is how we might implement a cancellable request where we will take the next request lyft.

[00:01:18] And then we'll race. Right, whichever one of these two things happens first, is what we're gonna go with. Either we'll get the lift back, or we'll get the request back, or we'll get the next cancel event, right? And at which point we'll no longer do anything when that API resolves, right?

[00:01:39] So we can actually have all sorts of different like race condition's concurrency. That's not really possible in a lot of other situations.