1 00:00:01,060 --> 00:00:07,010 In this video I want to show you one very quick optional refactor to fetch posts and users very quickly. 2 00:00:07,010 --> 00:00:08,740 This is a completely optional section. 3 00:00:08,750 --> 00:00:13,070 So if you're tired of this action creator that's totally fine positive right now and move on to the 4 00:00:13,070 --> 00:00:13,830 next one. 5 00:00:14,210 --> 00:00:14,460 OK. 6 00:00:14,470 --> 00:00:16,760 So if you're still here let's get to it really quickly. 7 00:00:16,760 --> 00:00:22,100 You'll notice that inside the section creator we are calling map with Lloyd ash and the recalling unique 8 00:00:22,100 --> 00:00:22,830 with Lloyd ash. 9 00:00:22,940 --> 00:00:25,380 And we've also got a four page statement down here. 10 00:00:25,400 --> 00:00:31,760 Now if we wanted to we could optionally refactor this into a little compact little piece of code using 11 00:00:31,770 --> 00:00:33,090 loadout as well. 12 00:00:33,120 --> 00:00:34,940 Let me show you how we would do this. 13 00:00:34,930 --> 00:00:39,290 And I'm going to give myself a little bit of space here and then I'm going to call a very special method 14 00:00:39,320 --> 00:00:46,070 inside the load ash library and I say underscore dot chain chain is a very special function inside of 15 00:00:46,070 --> 00:00:52,730 load ash that allows us to chain on essentially a bunch of additional functions that are going to manipulate 16 00:00:52,730 --> 00:00:54,470 some collection of data. 17 00:00:54,500 --> 00:00:56,890 Let me show you what I mean inside of chain. 18 00:00:56,900 --> 00:01:00,040 I'm going to pass the results of get state posts. 19 00:01:00,080 --> 00:01:03,570 So remember that is our list appose. 20 00:01:03,590 --> 00:01:08,270 Now I'm going to chain on our maps statement that we had put right here previously. 21 00:01:08,380 --> 00:01:14,960 So I going to say map like so when we change something and then chain on or call it was Sooni when we 22 00:01:14,960 --> 00:01:19,010 call the chain method and then chain on some additional method. 23 00:01:19,070 --> 00:01:24,830 The first argument to this map function right here behind the scenes will be whatever object we are 24 00:01:24,830 --> 00:01:26,030 changing over. 25 00:01:26,120 --> 00:01:30,650 So you can kind of imagine that behind the scenes this list of posts right here will be provided as 26 00:01:30,650 --> 00:01:32,750 the first argument to the map function. 27 00:01:32,750 --> 00:01:38,030 So then we only have to provide the second or third or fourth arguments that we want to pass in here. 28 00:01:38,030 --> 00:01:43,280 So in this case we had originally called the map function with our list of posts and a second argument 29 00:01:43,310 --> 00:01:44,740 of user ID. 30 00:01:44,740 --> 00:01:48,760 So now that list appose is going to be passed behind the scenes as the first argument to map. 31 00:01:48,860 --> 00:01:55,010 So we only have to pass in the second argument that we cared about of user id like so and now the beauty 32 00:01:55,010 --> 00:02:01,190 of chain is that we can take the result of that map step we can chain on another method right here and 33 00:02:01,190 --> 00:02:06,380 whatever result we got from the map step of the past into this next method. 34 00:02:06,380 --> 00:02:11,030 So originally we had called Map and then we passed the results of that into unique. 35 00:02:11,030 --> 00:02:15,440 So now I can chain on that unique like so. 36 00:02:15,540 --> 00:02:21,760 So the results of the mapping step will be automatically passed as the first argument into unique. 37 00:02:21,770 --> 00:02:22,220 All right. 38 00:02:22,220 --> 00:02:27,330 And so now we can do continue this process for the for each statement right here as well. 39 00:02:27,710 --> 00:02:32,110 So the result of unique will be passed into for each. 40 00:02:32,110 --> 00:02:37,590 And so now we could put a function inside of here that will be called for every unique ID. 41 00:02:37,660 --> 00:02:44,050 So I'm going to say look at each ID and dispatch the result of coaling fetch the user with that given 42 00:02:44,170 --> 00:02:48,710 ID and make sure you've got the three parentheses over here on the right hand side. 43 00:02:49,400 --> 00:02:49,840 OK. 44 00:02:49,860 --> 00:02:54,780 And now finally the last thing we do here this is a little bit of record keeping with load ash so to 45 00:02:54,780 --> 00:02:56,430 speak with load ash. 46 00:02:56,430 --> 00:03:04,620 It's not going to actually execute all these steps on a chained function like so until we put on a value 47 00:03:04,650 --> 00:03:08,080 step like so value a better term for value right. 48 00:03:08,130 --> 00:03:11,030 Might be something like execute or something like that. 49 00:03:11,040 --> 00:03:12,070 In other words load ASH. 50 00:03:12,090 --> 00:03:16,620 If you just list out all these Chene methods like so it's not going to actually execute all those steps 51 00:03:16,830 --> 00:03:20,880 until you finally put on a value like so. 52 00:03:20,930 --> 00:03:26,300 So we put on value to essentially say OK go ahead execute all these steps and map over those to post 53 00:03:26,420 --> 00:03:31,490 find the unique values and then run this function for each value inside there. 54 00:03:31,490 --> 00:03:36,860 So now we can delete all that stuff and we are left with just the chain statement. 55 00:03:37,620 --> 00:03:37,860 All right. 56 00:03:37,860 --> 00:03:38,460 And that's it. 57 00:03:38,460 --> 00:03:42,630 And I think you would agree with me this looks you know once you get over the chain stuff I think that 58 00:03:42,630 --> 00:03:44,070 this looks really clear. 59 00:03:44,100 --> 00:03:47,300 Very easy to understand and pretty darn straightforward. 60 00:03:47,470 --> 00:03:48,910 So let's do a quick save right here. 61 00:03:48,930 --> 00:03:51,660 We'll fall back on to our browser and test it out really quickly. 62 00:03:51,660 --> 00:03:55,100 So when I flip back over I still see my author names on on here. 63 00:03:55,260 --> 00:04:00,450 So there's Clementine and we got Leeanne up there and if I look at my network request log we still only 64 00:04:00,450 --> 00:04:03,480 have the 10 minimum requests we have to make. 65 00:04:03,480 --> 00:04:03,680 All right. 66 00:04:03,690 --> 00:04:07,830 So just a quick little refactor to make this action create a look a little bit better. 67 00:04:07,830 --> 00:04:09,320 So let's take a pause right here. 68 00:04:09,330 --> 00:04:10,730 We'll continue in the next video.