In this video I want to show you one very quick optional refactor to fetch posts and users very quickly.
This is a completely optional section.
So if you're tired of this action creator that's totally fine positive right now and move on to the
next one.
OK.
So if you're still here let's get to it really quickly.
You'll notice that inside the section creator we are calling map with Lloyd ash and the recalling unique
with Lloyd ash.
And we've also got a four page statement down here.
Now if we wanted to we could optionally refactor this into a little compact little piece of code using
loadout as well.
Let me show you how we would do this.
And I'm going to give myself a little bit of space here and then I'm going to call a very special method
inside the load ash library and I say underscore dot chain chain is a very special function inside of
load ash that allows us to chain on essentially a bunch of additional functions that are going to manipulate
some collection of data.
Let me show you what I mean inside of chain.
I'm going to pass the results of get state posts.
So remember that is our list appose.
Now I'm going to chain on our maps statement that we had put right here previously.
So I going to say map like so when we change something and then chain on or call it was Sooni when we
call the chain method and then chain on some additional method.
The first argument to this map function right here behind the scenes will be whatever object we are
changing over.
So you can kind of imagine that behind the scenes this list of posts right here will be provided as
the first argument to the map function.
So then we only have to provide the second or third or fourth arguments that we want to pass in here.
So in this case we had originally called the map function with our list of posts and a second argument
of user ID.
So now that list appose is going to be passed behind the scenes as the first argument to map.
So we only have to pass in the second argument that we cared about of user id like so and now the beauty
of chain is that we can take the result of that map step we can chain on another method right here and
whatever result we got from the map step of the past into this next method.
So originally we had called Map and then we passed the results of that into unique.
So now I can chain on that unique like so.
So the results of the mapping step will be automatically passed as the first argument into unique.
All right.
And so now we can do continue this process for the for each statement right here as well.
So the result of unique will be passed into for each.
And so now we could put a function inside of here that will be called for every unique ID.
So I'm going to say look at each ID and dispatch the result of coaling fetch the user with that given
ID and make sure you've got the three parentheses over here on the right hand side.
OK.
And now finally the last thing we do here this is a little bit of record keeping with load ash so to
speak with load ash.
It's not going to actually execute all these steps on a chained function like so until we put on a value
step like so value a better term for value right.
Might be something like execute or something like that.
In other words load ASH.
If you just list out all these Chene methods like so it's not going to actually execute all those steps
until you finally put on a value like so.
So we put on value to essentially say OK go ahead execute all these steps and map over those to post
find the unique values and then run this function for each value inside there.
So now we can delete all that stuff and we are left with just the chain statement.
All right.
And that's it.
And I think you would agree with me this looks you know once you get over the chain stuff I think that
this looks really clear.
Very easy to understand and pretty darn straightforward.
So let's do a quick save right here.
We'll fall back on to our browser and test it out really quickly.
So when I flip back over I still see my author names on on here.
So there's Clementine and we got Leeanne up there and if I look at my network request log we still only
have the 10 minimum requests we have to make.
All right.
So just a quick little refactor to make this action create a look a little bit better.
So let's take a pause right here.
We'll continue in the next video.