Instructor: I want to compare this approach where we click it, submit the request, wait for the request to succeed, and then update stay. We can see what that looks like here. We have some delays going on.
I want to contrast this to what we can achieve by pulling in the concept of optimistic UI updates, to where we click these, things happen immediately, and that asynchronously, we can account for failure and revert the state as needed. Let's take a look at implementing this. Let's rip this out. We know that we're going to receive a tweet ID here.
Let's log that out to the console to look at what we're working with. Here's our tweet ID, and if we click on these, we're logging into the console this tweet ID. Now, if we look at our state, what we're working with is we have this array of tweets, which is an array of objects with an ID, a number, which is the number of likes per tweet that we're generating here. You can see 03, 98, 0and so on.
The user name, which is what we're displaying here, and that tweet content. We're already mapping through that and displaying that. We have a tweet component that's already built for us. Now, we have this function here which is going to act as a request. It's going to mimic making an HTTP request, where it's going to do some logging for us so we can get a feel for when that request begins, ends.
We're introducing this idea of making it fail for tweet ID three and four. Now, we can go ahead and plug that in here. What we can do is, let's call our like tweet request, and we'll pass in true for now. We can see that our request function is succeeding and failing as we expect and that we have a promise rejection that we've yet to handle.