- [Instructor] Another feature of the process object
is standard input and standard output.
These two objects offer us a way to communicate
with a process while it's running.
So what we're going to do next is we want to make sure
that we're in our global file
and that we're navigated to the right spot in the terminal,
get rid of the code that's here,
and replace this with process.stdout.write,
and here, we're just going to add Hello,
a couple of spaces, and let's try to run this.
So node global, and then we'll see Hello,
and then when we use stdout.write,
we're sending some strings to the terminal.
So here what we could do is add some new lines
with the \n,
and then this is going to add those lines for us.
What we want to do next is we want
to create an array of questions.
So here we'll say const questions,
and we'll add a few strings here.
So we'll say, What is your name?,
What would you rather be doing?,
and then we'll say,
What is your preferred programming language.
Excellent.
So we have our questions,
then we're going to create an empty array of answers.
So here is our answers array.
And then finally, we're going to create a function called ask.
So ask is going to take i in as an argument.
So i is going to stand in
for the index for one of these questions.
And here we go.
We're going to say process.stdout.write.
We'll add a couple new lines here, /n/n.
We actually don't need the space in between those.
And then, let's go ahead and say questions i, perfect.
And because we're using this template string, holy moly,
I need to make sure
that I'm making this a template string with back ticks.
And then finally, let's go ahead
and add a little prompt for ourselves,
we're going to say process.stdout.write,
and then we'll add this little caret
to indicate to ourselves
that we are trying to accept some sort of input.
Then finally, we're going to invoke the question.
So we'll say, ask answers.length.
And now let's try to run this again. Cool.
So notice that running the app
is going to ask the first question.
It prompts the user for an answer, and then it quits.
So we also leave the terminal out of whack
because we use standard output here.
So what we actually want to do is wait
until the user answers the question.
So what we can do is listen for a data event
on this object using a function.
So we're going to say process.stdin.on data,
then we'll pass in a function here with the argument data,
and here we'll say process.stdout.write data.toString.trim.
So now we can continue to call ask with answers.length,
and let's check this out.
It says, What is your name? Eve.
I'll say, that's my name, and it copies me.
Stop it, stop it. (laughs)
It's always copying me.
By adding the listener,
we start using node asynchronously.
So every other app we've run until now has run
through the command synchronously and quit,
leaving us back at the terminal prompt.
But this time the app is still running.
It's waiting for some input.
So in the next lesson,
I'm going to talk to you about how we can stop the process
when we're out of data and we're out of these prompts.