1 00:00:00,240 --> 00:00:05,250 In this video you're going to learn how to use the callback chaining pattern with your asynchronous 2 00:00:05,250 --> 00:00:07,080 note J S code. 3 00:00:07,080 --> 00:00:13,030 So right now in app that J S we do indeed have two asynchronous I O operations. 4 00:00:13,050 --> 00:00:17,370 The problem is that both are operating completely independently of the other. 5 00:00:17,460 --> 00:00:21,600 We fire both OP and they each get their response and just dump it to the console. 6 00:00:21,600 --> 00:00:25,870 That's actually not what we want with the callback chaining pattern. 7 00:00:25,950 --> 00:00:28,640 We're gonna be able to do one thing then the next. 8 00:00:28,650 --> 00:00:31,960 Which means we'll be able to geo code and address. 9 00:00:32,040 --> 00:00:38,620 Then we can take the coordinates and use that as the input for some other asynchronous I O operation. 10 00:00:38,640 --> 00:00:42,870 In this case we will pass the coordinates to forecast to get started. 11 00:00:42,870 --> 00:00:48,270 We need to take our call to forecast and move it to where we have the geo coding output. 12 00:00:48,270 --> 00:00:53,300 Where do we have that geo coding output in our callback function for geo code. 13 00:00:53,310 --> 00:00:59,220 So what I'm going to do is take this forecast call and cut it out copying it to the clipboard then up 14 00:00:59,220 --> 00:01:04,630 above I'm going to paste it right inside of the GEO code callback function. 15 00:01:04,650 --> 00:01:06,150 Now what do we have. 16 00:01:06,150 --> 00:01:08,470 Well we start with a call to geo code. 17 00:01:08,490 --> 00:01:13,740 We start this asynchronous operation and when it's done we know that the event loop is going to make 18 00:01:13,740 --> 00:01:16,860 sure our callback gets called from there. 19 00:01:16,860 --> 00:01:22,920 We're gonna kick off another asynchronous IO operation then we're gonna wait for that callback to finish 20 00:01:23,190 --> 00:01:27,240 and inside of here we'll have access to the final data. 21 00:01:27,270 --> 00:01:28,870 So this is callback chaining. 22 00:01:28,920 --> 00:01:34,630 We are chaining together multiple callbacks to do multiple things in a specific order. 23 00:01:34,650 --> 00:01:36,260 Now what do we have to do. 24 00:01:36,270 --> 00:01:43,170 Well we have to make sure that the data properties latitude and longitude get used as the input for 25 00:01:43,170 --> 00:01:44,550 the forecast function. 26 00:01:44,550 --> 00:01:50,070 Right here I'm gonna take our first argument where I have my static number and I'm going to remove that 27 00:01:50,310 --> 00:01:53,910 and instead I'm going to get the property off of data up above. 28 00:01:54,090 --> 00:01:57,240 So this is the success output from Geo code. 29 00:01:57,240 --> 00:02:03,630 Now we know that data has latitude long to dude and we also added that other property location all of 30 00:02:03,630 --> 00:02:11,480 which we could choose to use in this case we need the latitude property so data latitude as the first 31 00:02:11,570 --> 00:02:17,390 argument to forecast now as the second argument to forecast we want to use the longitude. 32 00:02:17,390 --> 00:02:20,100 So over here we're gonna go ahead and get that done. 33 00:02:20,240 --> 00:02:21,710 I'm gonna remove these static. 34 00:02:21,710 --> 00:02:28,440 No we have in place for that second argument and I'll replace it with data dot longitude. 35 00:02:28,570 --> 00:02:29,420 Perfect. 36 00:02:29,440 --> 00:02:36,040 Now that we have this in place the input for forecast comes from the output for geo code and the end 37 00:02:36,040 --> 00:02:41,620 result is that we're going to get the weather for Boston because that's what's passed into geo code 38 00:02:41,710 --> 00:02:42,770 up above. 39 00:02:42,790 --> 00:02:46,150 Let's go ahead and make sure this actually works as expected. 40 00:02:46,150 --> 00:02:49,710 Right here I'm going gonna save apt J S and down below. 41 00:02:49,720 --> 00:02:52,550 We're gonna run the file so node app dot J. 42 00:02:52,560 --> 00:02:53,820 S and what do we get. 43 00:02:53,830 --> 00:02:59,620 We're gonna get two pieces of output and you'll notice that the geo coding information printed first 44 00:02:59,850 --> 00:03:05,350 we got our data because things went well then down below we got our forecast because once again things 45 00:03:05,350 --> 00:03:06,420 went well. 46 00:03:06,430 --> 00:03:08,690 Now you'll notice something pretty suspicious. 47 00:03:08,710 --> 00:03:14,190 It currently says it is negative 18 degrees and that's in Fahrenheit in Boston. 48 00:03:14,290 --> 00:03:15,010 It's not. 49 00:03:15,010 --> 00:03:16,680 It is mid November. 50 00:03:16,720 --> 00:03:18,160 Clearly not that cold. 51 00:03:18,160 --> 00:03:24,280 The problem here is that I've actually reversed the order of latitude and longitude so the latitude 52 00:03:24,280 --> 00:03:30,220 comes second with the index of one and the longitude comes first with the index of zero. 53 00:03:30,220 --> 00:03:33,700 So that's going to actually bring our application to a working state. 54 00:03:33,700 --> 00:03:35,310 Sorry for the confusion there. 55 00:03:35,310 --> 00:03:38,780 And now we get a temperature that makes a bit more sense. 56 00:03:38,800 --> 00:03:40,200 I reran the program. 57 00:03:40,210 --> 00:03:45,580 We still get our data printing but we can see these properties are reversed to the correct ones and 58 00:03:45,580 --> 00:03:49,750 down below we get the forecast for Boston once again clear throughout the day. 59 00:03:49,930 --> 00:03:55,620 But now it's about 40 degrees so above freezing as opposed to minus 18. 60 00:03:55,660 --> 00:04:00,580 Now that we have the basics in place let's make sure we're still handling things like our errors. 61 00:04:00,580 --> 00:04:06,340 So right here if something goes wrong with GEO code we want to make sure to use the error message in 62 00:04:06,340 --> 00:04:07,480 some sort of way. 63 00:04:07,600 --> 00:04:11,470 And we want to make sure we don't run the forecast call down below. 64 00:04:11,470 --> 00:04:17,560 If something went wrong with GEO code we won't have the input necessary to actually run forecast. 65 00:04:17,560 --> 00:04:22,340 So right here we're going to use a condition if there is an error do something. 66 00:04:22,420 --> 00:04:23,870 What are we going to do for now. 67 00:04:23,890 --> 00:04:26,110 We'll continue to print it. 68 00:04:26,140 --> 00:04:28,060 Now the goal here is to print the error. 69 00:04:28,090 --> 00:04:32,170 But the goal is to also not run the other code down here. 70 00:04:32,200 --> 00:04:34,480 Now there are two ways we could get that done. 71 00:04:34,480 --> 00:04:37,050 One is to toss on and else clots. 72 00:04:37,060 --> 00:04:42,540 We put all of this code inside of else and we know that only one of the two will run. 73 00:04:42,610 --> 00:04:47,330 Another approach worth noting is that you could just use return inside of here. 74 00:04:47,440 --> 00:04:53,040 Return is going to stop the function execution after printing the error message to the console. 75 00:04:53,080 --> 00:04:55,070 And this is a very common pattern. 76 00:04:55,120 --> 00:04:58,930 So I'm going to use it here to give you a bit more exposure to it. 77 00:04:59,020 --> 00:05:05,800 Now if there is an error the function stops and the error prints otherwise down below we continue on. 78 00:05:05,800 --> 00:05:08,230 Now we don't need these console dot log calls. 79 00:05:08,230 --> 00:05:13,010 The next thing we want to do if we do have data is to call forecast. 80 00:05:13,030 --> 00:05:15,700 And that's exactly what we're currently doing. 81 00:05:15,700 --> 00:05:19,240 Now we know that things can go wrong with forecast as well. 82 00:05:19,240 --> 00:05:22,370 So let's go ahead and set up our if statement inside of there. 83 00:05:22,570 --> 00:05:28,270 If something goes wrong with forecast once again I'll use return to stop this callback function and 84 00:05:28,270 --> 00:05:32,510 I will just print the error message to the console so down below. 85 00:05:32,530 --> 00:05:37,630 This is where we put the code that runs if both requests worked. 86 00:05:37,630 --> 00:05:43,060 If we geo coded and we got the forecast we can put our final code right here. 87 00:05:43,060 --> 00:05:44,260 And what are we going to do here. 88 00:05:44,470 --> 00:05:46,920 Well the goal here is to print two things. 89 00:05:47,020 --> 00:05:53,100 I want to print the location property from Geo code so people know what they're seeing the weather for 90 00:05:53,440 --> 00:05:58,900 and I also want to obviously print the forecast which is the data that comes back from the forecast 91 00:05:58,900 --> 00:05:59,720 function. 92 00:05:59,830 --> 00:06:05,050 Both of those are going to print down below giving the user all of the information we were hoping to 93 00:06:05,050 --> 00:06:05,770 provide. 94 00:06:05,770 --> 00:06:09,610 Now the goal is to use the output from both geo code and forecast. 95 00:06:09,610 --> 00:06:13,420 The problem though is that the argument name is the same for each. 96 00:06:13,420 --> 00:06:20,420 So down below in our forecast callback function we're only ever gonna be able to access the data here. 97 00:06:20,440 --> 00:06:26,710 The data here is going to shadow over data up above all we have to do to address that is to come up 98 00:06:26,710 --> 00:06:29,320 with a unique name for one or both. 99 00:06:29,560 --> 00:06:33,730 So right here I could call this something like forecast data. 100 00:06:33,730 --> 00:06:38,500 Now I have data and forecast data there's unique names and I can access both. 101 00:06:38,500 --> 00:06:44,170 Down below I'm going to remove those example console dot log calls we had before and we're going to 102 00:06:44,170 --> 00:06:48,240 start by printing the location that is data dot location. 103 00:06:48,250 --> 00:06:52,850 The last property from the geo code output that we have not used yet. 104 00:06:52,930 --> 00:06:56,320 Then down below the location we're going to print the forecast. 105 00:06:56,320 --> 00:07:04,300 So console dot log forecast data now that we have this in place we have a pretty good setup in the terminal. 106 00:07:04,300 --> 00:07:07,670 We can run our application node space app dot and J. 107 00:07:07,670 --> 00:07:14,600 S and if I fire that command off what do I get I get my location information followed by my forecast. 108 00:07:14,620 --> 00:07:17,490 Now here I can see which Boston I'm looking for. 109 00:07:17,500 --> 00:07:22,570 So if I was looking for a Boston in a different state or a different country I would know that I'm not 110 00:07:22,570 --> 00:07:27,610 actually getting the weather for what I intended to get the weather for and I could refine my search 111 00:07:27,630 --> 00:07:30,170 result even more to get a different Boston. 112 00:07:30,520 --> 00:07:35,770 So after the location we have the forecast once again still clear throughout the day with a temperature 113 00:07:35,770 --> 00:07:40,930 of thirty nine point five five degrees and a zero percent chance of rain. 114 00:07:40,960 --> 00:07:46,930 So this is the final output we're going gonna have if things go as they were supposed to. 115 00:07:46,930 --> 00:07:53,590 The only thing left to do is to make sure that the user can actually provide the location without needing 116 00:07:53,590 --> 00:07:55,870 to modify the source code. 117 00:07:55,870 --> 00:08:02,140 So we want to get this value which is currently hardcoded in as a command line argument. 118 00:08:02,140 --> 00:08:07,090 Getting that done is going to be your challenge for the video and down below I have some challenged 119 00:08:07,090 --> 00:08:09,400 comments outlining what I'd like you to do. 120 00:08:09,520 --> 00:08:14,260 The big picture goal except a location via a command line argument. 121 00:08:14,260 --> 00:08:20,220 Now you're going to access this single value without using any module like yards. 122 00:08:20,320 --> 00:08:26,620 You can use the standard functionality we explored on process and that was the second video of Section 123 00:08:26,620 --> 00:08:27,350 3. 124 00:08:27,400 --> 00:08:29,460 So if you forget exactly how that worked. 125 00:08:29,470 --> 00:08:30,570 That's OK. 126 00:08:30,580 --> 00:08:34,400 Go back to the video to jog your memory or use the PDA guide. 127 00:08:34,480 --> 00:08:37,970 Then you can actually integrate it into the app here. 128 00:08:37,990 --> 00:08:43,720 Now once you have the argument you're going to use that string as the input for geo code. 129 00:08:43,930 --> 00:08:49,680 So instead of having these static String Boston you'll reference the value the user provided. 130 00:08:49,690 --> 00:08:53,740 Now you're obviously only going to do that if a location was provided. 131 00:08:53,830 --> 00:08:59,410 If you're not seeing any argument provided you can go ahead and print a message saying something like 132 00:08:59,410 --> 00:09:05,230 please provide an address and then you would not run geo code and you could just use and else and an 133 00:09:05,230 --> 00:09:07,370 if statement to get that done. 134 00:09:07,390 --> 00:09:10,840 Now the last thing is to test your work with a couple of locations. 135 00:09:10,930 --> 00:09:17,050 And this I will give you so down below it's going to look a bit like that snowed app dot J S there's 136 00:09:17,050 --> 00:09:21,260 not going to be anything fancy we're just going to provide the location right here. 137 00:09:21,310 --> 00:09:24,850 So if I wanted to search for Boston it would look like this. 138 00:09:24,940 --> 00:09:30,460 Now if I wanted to search for a search query that had two words I would have to put those in double 139 00:09:30,460 --> 00:09:33,130 quotes like new space York. 140 00:09:33,130 --> 00:09:38,290 So you want to make sure that both of these work and in the end of the day you're going to get this 141 00:09:38,680 --> 00:09:41,590 and this as the first argument value. 142 00:09:41,590 --> 00:09:47,230 So take some time to refer back to what we covered quite some time ago and use it in the application 143 00:09:47,230 --> 00:09:47,760 here. 144 00:09:47,890 --> 00:09:53,110 When you're done test with those two locations and make sure you get what at least look like accurate 145 00:09:53,170 --> 00:09:54,210 temperatures. 146 00:09:54,340 --> 00:10:02,530 All right take some time to knock that out and when you're done come back and click play. 147 00:10:02,540 --> 00:10:04,190 How would you do to get this done. 148 00:10:04,190 --> 00:10:09,090 You needed to use the ARG the property on process which we explored earlier. 149 00:10:09,110 --> 00:10:14,840 So if I just dump that to the console we can see what we get when we run commands like these process 150 00:10:14,900 --> 00:10:16,010 dot arg. 151 00:10:16,490 --> 00:10:22,400 I'm going to save the program down below I'm going to rerun one of those commands and we can see what's 152 00:10:22,460 --> 00:10:23,540 on that ARG. 153 00:10:23,540 --> 00:10:31,100 The variable right here as we talked about earlier the third value is the first thing we provided the 154 00:10:31,100 --> 00:10:34,370 first thing is the path to the node executable. 155 00:10:34,370 --> 00:10:37,730 The second thing is the path to the script we ran. 156 00:10:37,730 --> 00:10:42,610 And the third is in this case New York or in the case of the other command Boston. 157 00:10:42,650 --> 00:10:44,180 That is what we want. 158 00:10:44,180 --> 00:10:51,350 So right here you could have created a variable like address setting it equal to a process dot ARG v 159 00:10:51,560 --> 00:10:54,950 accessing the third item with the index of 2. 160 00:10:54,950 --> 00:11:00,680 So now we have access to the command line argument and we want to use it as the input for geo code down 161 00:11:00,680 --> 00:11:01,110 below. 162 00:11:01,520 --> 00:11:08,660 So instead of the static String Boston I'm going to grab whatever value is stored on the address variable. 163 00:11:08,660 --> 00:11:13,880 Now this doesn't account for if no location was provided but for the moment we can at least test things 164 00:11:13,910 --> 00:11:15,480 out down below. 165 00:11:15,560 --> 00:11:20,390 I'm going to rerun the exact same command using New York as my search term. 166 00:11:20,570 --> 00:11:22,190 If I run it what do I get. 167 00:11:22,310 --> 00:11:26,900 I get New York New York in the United States and I get the temperature for that. 168 00:11:26,900 --> 00:11:33,650 Now I could run a different location node app J S Philadelphia I don't have to put it in quotes because 169 00:11:33,650 --> 00:11:35,510 it's just a single word. 170 00:11:35,660 --> 00:11:40,520 And if I fire that off I get the weather for where I'm currently in filming this video. 171 00:11:40,520 --> 00:11:46,550 So now that we have that in place we want to make sure we only geo code if an address is provided. 172 00:11:46,550 --> 00:11:52,410 Otherwise we'll print some sort of little string that's going to be what needs to happen for step three. 173 00:11:52,520 --> 00:11:54,560 And we could get that done with an if statement. 174 00:11:54,560 --> 00:12:01,690 So right here if there is no address what are we going to do. 175 00:12:01,750 --> 00:12:11,070 Console log please provide an address otherwise inside of an else clause. 176 00:12:11,080 --> 00:12:12,920 We're gonna go ahead and run the code. 177 00:12:13,000 --> 00:12:17,990 We had down below so I'll take my geo code call from beginning all the way to end. 178 00:12:18,040 --> 00:12:23,820 I'm going to cut it out of its current location and I'm going to paste it right inside of else. 179 00:12:23,950 --> 00:12:27,760 So that takes care of steps one two and three. 180 00:12:27,760 --> 00:12:34,240 Now we're just going to test our work and make sure we do get the error message when no address is provided. 181 00:12:34,240 --> 00:12:38,770 I'm going to remove the challenge comments as well as a couple of those extra lines and I'm going to 182 00:12:38,770 --> 00:12:41,210 test things out so down below. 183 00:12:41,230 --> 00:12:46,110 Let's run it node space app dot J s when I do that I would expect to see. 184 00:12:46,120 --> 00:12:53,420 Please provide an address and that is exactly what I get now node app dot J S Dublin. 185 00:12:53,550 --> 00:12:57,890 I'm gonna fire that off and this time we do indeed get a valid location. 186 00:12:57,900 --> 00:13:01,720 Dublin Ireland and I do indeed get the temperature for that. 187 00:13:01,830 --> 00:13:08,430 Now if I was looking for a different Dublin I could be more specific inside of quotes like Dublin P.A.. 188 00:13:08,580 --> 00:13:15,040 That's where I grew up and if I fire that off now I'm getting Dublin in Pennsylvania in the United States. 189 00:13:15,060 --> 00:13:17,660 With that in place the challenge is all done. 190 00:13:17,670 --> 00:13:20,420 Now there are plenty of ways you could have solved this. 191 00:13:20,430 --> 00:13:26,430 You might not have used the exact same setup and that is a ok as long as you're getting similar results 192 00:13:26,430 --> 00:13:27,830 for those commands. 193 00:13:27,840 --> 00:13:29,620 That's where we're going to stop for this one. 194 00:13:29,640 --> 00:13:34,530 I'm excited to continue on in the next video so let's go ahead and jump right in.