- [Instructor] Remember the collectAnswers function
that we created in lesson two of this chapter?
I think we added it to the questions.js file,
oh yeah, so here on line 14 we created a function
that will collect answers from an array of questions.
This is a perfect case for a reusable function,
and we should perhaps move it to it's own module.
So, what I'm going to go ahead and do is come over here
to my file folder, and I'm going to create a new folder,
this second icon here is to create a file folder,
and I'm going to call it lib.
So, what we want to do is add
a collectAnswers function to this lib folder.
So, we're going to create this module by modifying the code
to the questions.js file, so I'm going to come in here
and delete that space between the line,
we're going to get rid of the questions for now.
Instead of naming the function collectAnswers,
what we're going to do is, on line seven call
module exports, so we're going to export the function
as a module, so our consumers
can name it whatever they want,
but we're going to call the file collectAnswers
because that's what this function is.
Then, the last thing I'm going to do is come down here
to where we actually use the file
and delete that code as well.
Great, so now I've reduced my reusable
collectAnswers function to it's own module.
The next thing I'm going to do is come over here to go to file,
and then Save As, and I'm going to go ahead
and save this file as collectAnswers.js,
and now that I've created my own module
I also want to add this module to the lib folder.
So, I'm going to go ahead and grab my collect answers over here
and just drag it and drop it inside of the lib folder,
and I will select yes for move.
And now I have a collectAnswers function
within our library folder.
So, the next thing I want to do is improve
the questions.js file to use this module.
So, I'm going to go into questions.js,
and from here we don't need the readline anymore.
We are incorporating the readline
into our collect answers module.
We also don't need the function anymore,
so I'm going to delete this code from line seven to line 21,
and all we need to do is invoke
the collect answers function,
so the other thing that we're going to need
to do is get a collectAnswers function.
Let's go ahead and say const
collectAnswers equals require,
and because this modules is within my local file system,
I have to specify where the file is,
so I'm going to start with ./ and it's inside
of the library folder, /collectAnswers.
Great, so now I've modified the questions.js file
that we created earlier in this chapter,
by moving collectAnswers into it's own module.
Let's go ahead and test it out.
So, I can open up the terminal,
and we'll say node questions.js,
and it's asking us the questions,
my name is Alex, I live in Tahoe City,
and I'm going to change the world with Node.js.
And, as we can see the functionality of this file
is the same, even though we've separated
the logic for collectAnswers into a separate module.
We are going to do one little thing to improve this module.
And this might seem a little silly,
but if we actually grab this function,
and get rid of it, let's say I were to put it
on my clipboard by doing a Cmd + x,
and I just sent the questions
to the collectAnswers function.
Maybe my users don't want to handle this when it's done,
maybe they just want to ask the questions for some reason.
Right now if I open my terminal,
and I run node questions.js,
we'll say "Alex", "Tahoe City",
and "Change the world" again, but this time I get an error.
And, I get an error because our callback is not a function,
meaning that when we called collectAnswers
we didn't send a function with our answers,
with our questions.
So, what I want to do is I want to make
this done callback optional for my users,
and there's a couple of ways I can do this.
So, this is the callback function here,
on line seven, and in our last example
we invoked this function without sending a done.
So, down here on line 16 is where we call it.
There's a couple of ways to skip this,
I could just put in another
if else statement if I wanted to.
But what I'm actually going to do is create a default function,
by setting done equal to f arrow f,
I create a default dummy function that won't break my code.
F arrow f doesn't do anything,
this function just returns the arguments
that are sent to it, but this won't break my code,
so when we get down to line 16
if done isn't supplied our code will not break.
So, we can go ahead and save this,
open up the terminal again, and try this out one more time.
So, I'll go ahead and clear,
and then we will node questions
Alex, Tahoe City, change the world,
and this time even though we didn't exit the process
when we're done, we're actually still waiting
for more responses from the terminal,
we made done optional, so we don't see an error there.
I can stop the process by hitting Ctrl + c,
and just for the fun of it,
I'm going to come back into questions,
and I'm going to go ahead and paste my handler function
in there again, so that we do stop
the process and say thank you for your answers.
But, now we've clearly made that second argument optional,
meaning that our collectAnswers function
still works without the callback.