In this video you're going to create your very first asynchronous non blocking no J.S. application and
by non blocking I just mean that your application is going to continue to be able to do other things
while it's waiting for some long running Io process to complete.
And that's one of the things that makes no J.S. so great fast and efficient.
So in this video we're going to explore asynchronous programming with a basic example.
Then we'll take what we learn here about asynchronous programming and use it to build out our weather
application to actually get that app built.
We're gonna need to interact with real time weather API is and that is going to require us to make a
synchronous H TTP requests.
All right let's go ahead and get started with a basic example to start.
Let's go ahead and close all open editors and then down below.
I'm also going to collapse that Notes app directory and we're going to create a new folder for our new
project right here inside of the node course folder.
I'm going to create a new one and I'll call this whether hyphen app and this is the directory we're
going to end up working out of throughout the next several sections to kick things off.
We're going to create a single file inside of here and obviously we will expand on that as we build
out the application but for the moment a single script called app dot J S is gonna give us all we need
to explore the basics of asynchronous programming for the moment.
We don't even need to initialize NPM as in this video.
We're not going to need any NPM modules.
Let's get started with a few well-placed console dialog calls.
I'm going to add one at the top of our script before all of the code.
We're going to write and this will just say starting and then we're gonna add a second one.
After all of the code we're going to write and console dot lug this one can say something like Stop
it everything else we're going to write is gonna go right in between now.
Currently we have a completely synchronous program in a synchronous programming model.
One line runs after the next regardless of how long each line takes to execute.
Now in this case we have a very simple program where starting is going to run first and stopping would
run after no shock there.
Now let's go ahead and actually explore how we could add a bit of asynchronous programming into this
script and to do that we're going to use one of the most basic asynchronous functions that node provides
and that is set time out set time out allows us to run some code after a specific amount of time has
passed and set time out is indeed a function.
So let's call it as such and it takes in to arguments in both are required.
The first is a function.
So let's go ahead and for the moment just set up an arrow function right here like this.
And the second is a number.
It's the number of milliseconds you want to wait before the callback gets executed.
So in our case let's go ahead and use two thousand milliseconds which would equal to two seconds.
So what we're really saying is hey here's a function can you run this function after two seconds have
passed.
Now let's add something inside of this function.
The one we passed to set time out so we can actually see that it gets called when it's supposed to.
And for the moment as you might have expected we're just going to use console dot log to print a message
right here two second timer.
And by having all of these logs in place we're going to be able to see how our program executes by running
the script down below.
Now in a synchronous programming model one thing happens after the next.
So we would have the starting message print.
Then we would wait two seconds and we would see our two second timer message print.
And finally we would see the stopping message print with asynchronous no J.S. that is not the order
we're gonna get down below in the terminal.
I'm gonna use C D to change directories into the weather app.
Go ahead and do the same.
And once we're here we're just going to run app and J.S. through node so node space app dot J.
S Now we'll run it a couple of times because things are gonna happen pretty quick here so I'm gonna
run it and what happens we see starting prints then stopping prints then the program just kind of hangs
for two seconds followed by the two second timer printing.
Let's go ahead and do that one more time right here.
You'll notice we're not getting brought back to the command prompt until those two seconds pass.
So now our node application does indeed take over two seconds to run.
Then we're brought back to the command prompt where we could do something else.
So that node process is going to require just over two seconds.
And now you'll notice the order that we're getting those messages in we have starting printing first
which still makes a lot of sense.
Then we have stopping and finally we have the two second timer printing.
So in these synchronous model we needed to wait those two seconds before the program would continue
in an asynchronous non blocking model node can continue to do other things like run everything down
below.
While it's waiting for those two seconds to pass.
Now in this case we're using something simple like set time out to illustrate the basics of asynchronous
programming.
But imagine this was a database request if we were fetching data for one user.
It would be nice to be able to do other things in our program.
While we're waiting the couple hundred milliseconds it takes to get that information back.
Otherwise node would be terrible at its job.
Instead with the non blocking model we're able to do a lot of things at the same time.
I can wait for 60 database requests to come back while still issuing more.
Now let's take things one step further by adding a second set time out call into the mix and generating
some strange and potentially unexpected results.
All right here I am going to add a second set time out call passing in these same two arguments.
The first is our function to run after a specific amount of time has passed and the second argument
is the amount of time to wait in milliseconds.
And in this case we're going to wait 0 milliseconds saying we want to run the function right away.
Now inside of here once again we're just going to print a message so we can see the order in which things
happen.
So console log and I will print zero second timer to the console.
I'm going to save about J.
S..
And down below I will use clear to clear the terminal output before rerunning the app dot J s script.
From here I can cycle through my previous commands to rerun the program and let's go ahead and look
at the order in which things happen.
Right here we see starting then stopping then zero second timer followed by two second timer.
Now I definitely expected the two second timer to print after two seconds but why is zero second timer
coming after stopping.
If I'm not waiting any time at all it would seem that I would get this message before I would get the
message down below.
But clearly that's not the case.
Now to explore why we're going to take a look at the internals of node j ask that are responsible for
actually getting asynchronous programming to work and we're going to do that in the next video with
a little visualization.
I put together that goes over a few examples like this one showing you exactly how they run behind the
scenes.
That's gonna give us a better mental model for working with asynchronous programming and then any video.
After that we'll start the process of making HDTV requests so we can fetch real time weather data.
I'm excited to talk about why things are working the way they're working in the next one so let's go
ahead and jump right in.