- [Instructor] Being able to ask a single question
to ourself is pretty cool, but I think
we can take this a step further.
So I want to create a function
as well as a list of questions.
Let's start with the questions.
We'll say, "what is your name?"
We will say, "where do you live?"
And "what are you going to do with Node.js?"
Node.js, what are we going to do with you?
So the next thing we'll do is create
a function called collectAnswers.
Now this function, collectAnswers takes
in our questions array as well as a function called done
that's going to fire when we're done asking questions.
Now, the next thing we'll do is initialize
an empty array to contain these answers,
then we're going to create this other function,
called questionAnswered.
This will take in an answer,
and we're going to push these into the answers array,
and then we'll also trim off
the answer white space if it exists.
Okay, so some familiar code incoming,
if answers.length is less than questions.length,
then we want to ask another question,
and we'll say questions, answers.length, questionAnswered.
So check this out.
Let's collapse our terminal for a second.
We're going to remember that this
question value takes in two things.
So it takes in a question, which we add here as a string,
but here we're adding as a position in an array,
and then it takes in this callback function.
So what we're saying is here
call this function again with this value.
We're only going to do this
in cases where our answers array length is less
than our questions array length, but if it's not,
if we're out of questions, then we're going
to return this function called done with our answers.
So the done function, we're going to come back to in a second
but just think of this as being a function,
kind of like an exit function that's going
to be called in the event that we're out of questions.
The final thing we want to do here is we're going
to take our rl question,
and we're going to replace these values.
So the first value is our question, right?
So we can say questions, zero,
and then our callback function, you guessed it,
is going to be questionAnswered.
So notice that this is outside
of the questionAnswered function,
and it's inside of the collectAnswers function.
So now that we've created this,
we can call collectAnswers.
The first thing that we'll pass in here is questions.
So remember, collectAnswers takes
in our array of questions right here,
and then it also takes in this function called done.
So it's up to us now to define
what this done function will do.
When we're out of questions,
we want to say "thank you for your answers."
This is a friendly app we're building.
We'll console-log all of those answers,
and then we'll call process.exit.
So let me scroll down a little bit.
We're going to take a look at how this looks.
We want to make sure that we're
in the right folder,
and we are.
So now we can run node ask.
"What is your name? Where do you live?
What are you going to do with Node.js?"
Do good things, make good profits.
All right, so this is giving us an error,
"answers is not defined,"
and that's because I didn't pass it in here.
Holy moly, you were probably yelling
at your video when I didn't do that,
and now I can try rerunning that again.
We're going to say have fun,
make friends, build cool things.
So now, all of our answers are being locked.
So this is a great example
of a few common patterns that exist within Node.js.
The first of which is that we're constantly
sending functions to other functions as arguments.
So we're doing this in the case of our done function,
and this is going to help us do something asynchronously.
I don't want to just call done at any old moment.
I want to call the done function
when we're out of questions.
When we're done asking these questions,
I want to call this function,
and we define this function inside
of this collectAnswers function as an argument.
The first thing it takes in is our questions,
and then we also have our answers.