Current section: Client Router 27 exercises
Problem

Race Conditions

Loading exercise

Transcript

00:00 What we're going to talk about here is a bit of a optimization that typically your framework is going to manage for you, and so we don't normally need to go too deep on these types of optimizations for your understanding of React server components.

00:14 But I just couldn't let myself let you make this mistake or forget about thinking about race conditions. And so we're going to think about race conditions. If we open up our app.js file in the server, we're going to find that our friend Kelly,

00:31 the coworker, has added this little bit of code to help us simulate a race condition. So if your search query starts with ST or is ST, then this particular event or request

00:47 is going to take a little bit longer than the other requests. And so the benefit of this being we can just test out this race condition by typing star into the filter, and you'll see that the response comes back wrong. And so like this is kind of simulating how sometimes a network request can come into

01:05 your server, and maybe it's really busy with something, and so that ties things up. And then another request comes in, and maybe it goes to a different server, your horizontally scaling or just the powers that be with computers, that request comes back first. So that describes basically a race condition. So we're simulating that right here.

01:25 And if I type in star, you'll notice my search gets updated to ST. So once this response comes back, then our promise chain continues and we end up updating the URL. So we don't want to do that. So your job is to handle the race condition properly so that we don't have this problem

01:45 with the URL. It's actually just a couple lines of code changed in our UI index. So let's get into it.