- [Instructor] Readline is the module
that we can use to help us build an application
that would ask questions of a terminal user.
It's an interface around readable and writable
streams that allows us to easily write code
that would prompt the user and collect their answers.
Let's take a look at using the readline module.
I'm going to go ahead and add another file
to the directory here and I'm going to call it ask dot JS.
And within this file we'll first load
the readline module using the require function.
And readline is one of those core modules
that's available to you without having to install
anything other than node JS.
In order to work with readline we must create
an interface.
So I'm going to create a variable called RL
that we will use as our readline interface.
And I can call readline dot create interface.
So within the create interface function
we need to supply an object that tells readline
what stream to use for input and what stream
to use for output.
We're going to use process dot std in for input
and output process dot std out.
This means that we can ask questions and collect
input from the terminal.
So now to ask a question we can use the question
function that's available to us on our interface.
So RL question how do you like node?
The first argument is the string for the question
that we should ask and the second argument
is a callback function for once that question
has been answered.
The answer will be supplied as an argument
to that callback function.
So as soon as we get the answer I'll go ahead
and console log your answer and then we'll
go ahead and do answer.
So now if I save this and then open up
the terminal and node ask, you will notice
that our question has been asked of us,
how do you like node?
We have an asynchronous process running
because we're waiting for my answer before
the callback function is invoked.
So I can say I love it and as soon as I hit enter
we will invoke that callback function and it says
your answer, I love it.
So this is an example of how a core module
will help us write code that's a little bit
easier to work with in order to get the job done.