In this video you're going to learn how to use the callback chaining pattern with your asynchronous
note J S code.
So right now in app that J S we do indeed have two asynchronous I O operations.
The problem is that both are operating completely independently of the other.
We fire both OP and they each get their response and just dump it to the console.
That's actually not what we want with the callback chaining pattern.
We're gonna be able to do one thing then the next.
Which means we'll be able to geo code and address.
Then we can take the coordinates and use that as the input for some other asynchronous I O operation.
In this case we will pass the coordinates to forecast to get started.
We need to take our call to forecast and move it to where we have the geo coding output.
Where do we have that geo coding output in our callback function for geo code.
So what I'm going to do is take this forecast call and cut it out copying it to the clipboard then up
above I'm going to paste it right inside of the GEO code callback function.
Now what do we have.
Well we start with a call to geo code.
We start this asynchronous operation and when it's done we know that the event loop is going to make
sure our callback gets called from there.
We're gonna kick off another asynchronous IO operation then we're gonna wait for that callback to finish
and inside of here we'll have access to the final data.
So this is callback chaining.
We are chaining together multiple callbacks to do multiple things in a specific order.
Now what do we have to do.
Well we have to make sure that the data properties latitude and longitude get used as the input for
the forecast function.
Right here I'm gonna take our first argument where I have my static number and I'm going to remove that
and instead I'm going to get the property off of data up above.
So this is the success output from Geo code.
Now we know that data has latitude long to dude and we also added that other property location all of
which we could choose to use in this case we need the latitude property so data latitude as the first
argument to forecast now as the second argument to forecast we want to use the longitude.
So over here we're gonna go ahead and get that done.
I'm gonna remove these static.
No we have in place for that second argument and I'll replace it with data dot longitude.
Perfect.
Now that we have this in place the input for forecast comes from the output for geo code and the end
result is that we're going to get the weather for Boston because that's what's passed into geo code
up above.
Let's go ahead and make sure this actually works as expected.
Right here I'm going gonna save apt J S and down below.
We're gonna run the file so node app dot J.
S and what do we get.
We're gonna get two pieces of output and you'll notice that the geo coding information printed first
we got our data because things went well then down below we got our forecast because once again things
went well.
Now you'll notice something pretty suspicious.
It currently says it is negative 18 degrees and that's in Fahrenheit in Boston.
It's not.
It is mid November.
Clearly not that cold.
The problem here is that I've actually reversed the order of latitude and longitude so the latitude
comes second with the index of one and the longitude comes first with the index of zero.
So that's going to actually bring our application to a working state.
Sorry for the confusion there.
And now we get a temperature that makes a bit more sense.
I reran the program.
We still get our data printing but we can see these properties are reversed to the correct ones and
down below we get the forecast for Boston once again clear throughout the day.
But now it's about 40 degrees so above freezing as opposed to minus 18.
Now that we have the basics in place let's make sure we're still handling things like our errors.
So right here if something goes wrong with GEO code we want to make sure to use the error message in
some sort of way.
And we want to make sure we don't run the forecast call down below.
If something went wrong with GEO code we won't have the input necessary to actually run forecast.
So right here we're going to use a condition if there is an error do something.
What are we going to do for now.
We'll continue to print it.
Now the goal here is to print the error.
But the goal is to also not run the other code down here.
Now there are two ways we could get that done.
One is to toss on and else clots.
We put all of this code inside of else and we know that only one of the two will run.
Another approach worth noting is that you could just use return inside of here.
Return is going to stop the function execution after printing the error message to the console.
And this is a very common pattern.
So I'm going to use it here to give you a bit more exposure to it.
Now if there is an error the function stops and the error prints otherwise down below we continue on.
Now we don't need these console dot log calls.
The next thing we want to do if we do have data is to call forecast.
And that's exactly what we're currently doing.
Now we know that things can go wrong with forecast as well.
So let's go ahead and set up our if statement inside of there.
If something goes wrong with forecast once again I'll use return to stop this callback function and
I will just print the error message to the console so down below.
This is where we put the code that runs if both requests worked.
If we geo coded and we got the forecast we can put our final code right here.
And what are we going to do here.
Well the goal here is to print two things.
I want to print the location property from Geo code so people know what they're seeing the weather for
and I also want to obviously print the forecast which is the data that comes back from the forecast
function.
Both of those are going to print down below giving the user all of the information we were hoping to
provide.
Now the goal is to use the output from both geo code and forecast.
The problem though is that the argument name is the same for each.
So down below in our forecast callback function we're only ever gonna be able to access the data here.
The data here is going to shadow over data up above all we have to do to address that is to come up
with a unique name for one or both.
So right here I could call this something like forecast data.
Now I have data and forecast data there's unique names and I can access both.
Down below I'm going to remove those example console dot log calls we had before and we're going to
start by printing the location that is data dot location.
The last property from the geo code output that we have not used yet.
Then down below the location we're going to print the forecast.
So console dot log forecast data now that we have this in place we have a pretty good setup in the terminal.
We can run our application node space app dot and J.
S and if I fire that command off what do I get I get my location information followed by my forecast.
Now here I can see which Boston I'm looking for.
So if I was looking for a Boston in a different state or a different country I would know that I'm not
actually getting the weather for what I intended to get the weather for and I could refine my search
result even more to get a different Boston.
So after the location we have the forecast once again still clear throughout the day with a temperature
of thirty nine point five five degrees and a zero percent chance of rain.
So this is the final output we're going gonna have if things go as they were supposed to.
The only thing left to do is to make sure that the user can actually provide the location without needing
to modify the source code.
So we want to get this value which is currently hardcoded in as a command line argument.
Getting that done is going to be your challenge for the video and down below I have some challenged
comments outlining what I'd like you to do.
The big picture goal except a location via a command line argument.
Now you're going to access this single value without using any module like yards.
You can use the standard functionality we explored on process and that was the second video of Section
3.
So if you forget exactly how that worked.
That's OK.
Go back to the video to jog your memory or use the PDA guide.
Then you can actually integrate it into the app here.
Now once you have the argument you're going to use that string as the input for geo code.
So instead of having these static String Boston you'll reference the value the user provided.
Now you're obviously only going to do that if a location was provided.
If you're not seeing any argument provided you can go ahead and print a message saying something like
please provide an address and then you would not run geo code and you could just use and else and an
if statement to get that done.
Now the last thing is to test your work with a couple of locations.
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
not going to be anything fancy we're just going to provide the location right here.
So if I wanted to search for Boston it would look like this.
Now if I wanted to search for a search query that had two words I would have to put those in double
quotes like new space York.
So you want to make sure that both of these work and in the end of the day you're going to get this
and this as the first argument value.
So take some time to refer back to what we covered quite some time ago and use it in the application
here.
When you're done test with those two locations and make sure you get what at least look like accurate
temperatures.
All right take some time to knock that out and when you're done come back and click play.
How would you do to get this done.
You needed to use the ARG the property on process which we explored earlier.
So if I just dump that to the console we can see what we get when we run commands like these process
dot arg.
I'm going to save the program down below I'm going to rerun one of those commands and we can see what's
on that ARG.
The variable right here as we talked about earlier the third value is the first thing we provided the
first thing is the path to the node executable.
The second thing is the path to the script we ran.
And the third is in this case New York or in the case of the other command Boston.
That is what we want.
So right here you could have created a variable like address setting it equal to a process dot ARG v
accessing the third item with the index of 2.
So now we have access to the command line argument and we want to use it as the input for geo code down
below.
So instead of the static String Boston I'm going to grab whatever value is stored on the address variable.
Now this doesn't account for if no location was provided but for the moment we can at least test things
out down below.
I'm going to rerun the exact same command using New York as my search term.
If I run it what do I get.
I get New York New York in the United States and I get the temperature for that.
Now I could run a different location node app J S Philadelphia I don't have to put it in quotes because
it's just a single word.
And if I fire that off I get the weather for where I'm currently in filming this video.
So now that we have that in place we want to make sure we only geo code if an address is provided.
Otherwise we'll print some sort of little string that's going to be what needs to happen for step three.
And we could get that done with an if statement.
So right here if there is no address what are we going to do.
Console log please provide an address otherwise inside of an else clause.
We're gonna go ahead and run the code.
We had down below so I'll take my geo code call from beginning all the way to end.
I'm going to cut it out of its current location and I'm going to paste it right inside of else.
So that takes care of steps one two and three.
Now we're just going to test our work and make sure we do get the error message when no address is provided.
I'm going to remove the challenge comments as well as a couple of those extra lines and I'm going to
test things out so down below.
Let's run it node space app dot J s when I do that I would expect to see.
Please provide an address and that is exactly what I get now node app dot J S Dublin.
I'm gonna fire that off and this time we do indeed get a valid location.
Dublin Ireland and I do indeed get the temperature for that.
Now if I was looking for a different Dublin I could be more specific inside of quotes like Dublin P.A..
That's where I grew up and if I fire that off now I'm getting Dublin in Pennsylvania in the United States.
With that in place the challenge is all done.
Now there are plenty of ways you could have solved this.
You might not have used the exact same setup and that is a ok as long as you're getting similar results
for those commands.
That's where we're going to stop for this one.
I'm excited to continue on in the next video so let's go ahead and jump right in.