- [Instructor] In the last lesson,
we used Node.js asynchronously by using event listeners.
So what we said here was instead of just running
the functions when they are added to our JavaScript file,
we said only call the function that's sent
to this function when we have a data event,
and only send this function when we have an exit event.
So call these functions at specific times.
Now, another way that we can work
with Node.js asynchronously is through timing functions.
So the timing functions like set timeout,
clear timeout, set interval,
and clear interval work the same way
that they do in the browser,
and are available to us globally.
So let's go ahead and create a set timeout function.
We're going to create a new variable here called waitTime.
So we'll say const waitTime.
And we'll set that to 3,000,
which means three seconds or 3,000 milliseconds.
Then we're going to call a console log,
and we'll say setting a waitTime.
And we'll divide that by 1,000
so we can get the number of seconds.
And we'll say second delay,
setting a something second delay.
Then we're going to create a function called timer finished.
This is going to be set too.
We'll use an arrow function here.
An arrow function is just going to point
directly at what we want to return
which is another console log message saying done.
Now here's where the set timeout comes into play.
This is the cool part.
Set timeout is going to take two in things.
The first is a function that we're going to call
whenever the timeout has elapsed.
So whenever that three-second delay
has occurred, timerFinished.
Then we'll plug in the wait time as the second argument
to define how long it should take.
Perfect.
We're going to run it by saying node global.
And now, it'll say the setting a three-second delay,
and we're done.
So we console log this message first,
we wait the requisite time, and then we call this function.