Irvan
Irvan

Reputation: 175

Node Js Function Declaration and Expression

I just started to learn Node Js then I got the following code in the tutorial

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const questions = [
  "What is your name? ",
  "Where do you live? ",
  "What are you going to do with node js? "
];

const collectAnswers = (questions, done) => {
  const answers = [];
  const [firstQuestion] = questions;

  const questionAnswered = answer => {
    answers.push(answer);
    if (answers.length < questions.length) {
      rl.question(questions[answers.length], questionAnswered);
    } else {
      done(answers);
    }
  };

  rl.question(firstQuestion, questionAnswered);
};

collectAnswers(questions, answers => {
  console.log("Thank you for your answers. ");
  console.log(answers);
  process.exit();
});

The code has the following result

What is your name? a
Where do you live? b
What are you going to do with node js? c
Thank you for your answers.
[ 'a', 'b', 'c' ]

As far as I understand that the variable collectAnswer somehow inject the function declared below to the second parameter (done). Can someone explain what is actually happening behind the scene? How can the function declared below injected to the variable with the same name with it? Any term this pattern actually called?

Upvotes: 0

Views: 376

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074335

Functions are objects in JavaScript, unlike some other languages, so you can pass them into functions as arguments and the functions can receive them as parameters. This is done a lot so that the function you're calling can "call back" to your code (the function you're passing in). A function used this way is often called a "callback." A function like collectAnswers that accepts a function it's going to call this way is often described as a function that "accepts a callback."

In that code, there's a function called collectAnswers and it accepts two parameters, questions and done:

const collectAnswers = (questions, done) => {
//                      ^^^^^^^^^−−^^^^−−−−−−−−−−−−−−−− parameters
    // ...
};

At the end, the code calls collectAnswers, passing in an array of questions and new function created inline as the second argument (I've put the two arguments on separate lines for clarity):

collectAnswers(
    questions,                                          // 1st argument

    answers => {                                        //
        console.log("Thank you for your answers. ");    //
        console.log(answers);                           // 2nd argument
        process.exit();                                 //
    }                                                   //
);

That does the same thing as this, except for the callback constant:

const callback = answers => {
    console.log("Thank you for your answers. ");
    console.log(answers);
    process.exit();
};
collectAnswers(questions, callback);

Upvotes: 2

Related Questions