- [Instructor] In the last lesson,
we started working with Node.js asynchronously
by using event listeners.
Another way we can work with Node.js asynchronously
is through the timing functions.
The timing functions,
set timeout, clear timeout,
set interval, and clear interval,
work the same way they do in the browser
and they're available to you globally.
Let's go ahead and create a three second delay
using a set timeout function.
I'm going to come over to our folder
and add a new JavaScript file called Timers.js.
And our wait time will equal 3,000 milliseconds,
which is three seconds.
So then we will go ahead and log a message
using the template string.
So using the back tick character.
So in this console.log here,
we're going to take the wait time and divide it by a thousand,
which will give us the number of seconds
that we're waiting.
Now it's time to create a function to invoke
when the timer is finished.
So when the timer is finished,
we will add this arrow function
and because this arrow function's
only going to contain one line
I don't have to use the brackets.
So, I'll do console.log done.
So here is a one line arrow function that
when we invoke it, it will just simply log the term done
to the console.
So now I'm ready to use the set timeout
and when you set a timeout
the first argument is the function to invoke
when the timer is completed.
Timer finished will be that function.
The second argument that we're going to use
is the time that we should wait for this delay.
So I'll go ahead and add the wait time.
There we go.
So now this process will run asynchronously
for three seconds.
Once we've waited for three seconds,
the timer finished function will be invoked
and we should see the term done logged to the console.
Let's go ahead and open up our terminal
and run our application.
Node timers
and we see our message
and after three seconds we see the term done
logged to the console.