Running more than one function in Node.js

huangapple go评论69阅读模式
英文:

Running more than one function in Node.js

问题

I'm having trouble understanding why my application will terminate after running only one function. I have coded the application as follows...

const readline = require('readline');

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

console.log('Welcome. This application will check if a given year is a leap year.');

checkYear();

function checkYear() {
   io.question('Check which year? ', function (year) {
      year % 100 === 0 && year % 400 !== 0
         ? false
         : year % 4 === 0
         ? console.log(year + ' is a leap year.')
         : console.log(year + ' is not a leap year.');
      });

   io.question('Check another year? y/n'), function(response) {
      if (response.toLowerCase === 'y') {
         checkYear();
      } else {
         io.close();
      }
   }
};

我也尝试将第二个提示分成自己的函数,并连续调用checkYear和checkAgain,但我得到了相同的结果。我需要做什么来使io.question('Check another year')提示在应用程序响应第一个输入后运行?

英文:

I am having trouble understanding why my application will terminate after running only one function. I have coded the application as follows...

const readline = require('readline');

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

console.log(
  'Welcome. This application will check if a given year is a leap year.'
);

checkYear();

function checkYear() {
   io.question('Check which year? ', function (year) {
      year % 100 === 0 && year % 400 !== 0
         ? false
         : year % 4 === 0
         ? console.log(year + ' is a leap year.')
         : console.log(year + ' is not a leap year.');
      });

   io.question('Check another year? y/n'), function(response) {
      if (response.toLowerCase === 'y') {
         checkYear();
      } else {
         io.close();
      }
   }
};

I have also tried breaking the second prompt into its own function and calling both checkYear and checkAgain back-to-back, but I get the same result. What do I need to do to make the io.question('Check another year') prompt run after the application responds to the first input?

答案1

得分: 2

I believe you are expecting another input with 2nd io.question call.
In your code, 2nd io.question will be executed immediately after the 1st io.question without waiting for the user to enter anything.

So, you will need to nest the 2nd call inside the 1st call:

function checkYear() {
  io.question("Check which year? ", function (year) {
    year % 100 === 0 && year % 400 !== 0
      ? false
      : year % 4 === 0
      ? console.log(year + " is a leap year.")
      : console.log(year + " is not a leap year.");

    io.question("Check another year? y/n", function (response) {
      if (response.toLowerCase() === "y") {
        checkYear();
      } else {
        io.close();
      }
    });
  });
}

Other than that, a couple of typo mistakes are:

  • should be
io.question("Check another year? y/n", function (response) {
...
});

NOT

io.question('Check another year? y/n'), function(response) {
...
}
  • should be
    response.toLowerCase() === "y"
    NOT
    response.toLowerCase === 'y'

You can refer to my sandbox: https://codesandbox.io/p/sandbox/friendly-lake-b15kkz
Hope this helps Running more than one function in Node.js

英文:

I believe you are expecting another input with 2nd io.question call.
In your code, 2nd io.question will be executed immediately after the 1st io.question without waiting for user to enter anything.

So, you will need to nest the 2nd call inside the 1st call:

function checkYear() {
  io.question("Check which year? ", function (year) {
    year % 100 === 0 && year % 400 !== 0
      ? false
      : year % 4 === 0
      ? console.log(year + " is a leap year.")
      : console.log(year + " is not a leap year.");

    io.question("Check another year? y/n", function (response) {
      if (response.toLowerCase() === "y") {
        checkYear();
      } else {
        io.close();
      }
    });
  });
}

Other than that, couple of typo mistakes are:

  • should be
io.question("Check another year? y/n", function (response) {
...
});

NOT

io.question('Check another year? y/n'), function(response) {
...
}
  • should be
    response.toLowerCase() === "y"
    NOT
    response.toLowerCase === 'y'

You can refer to my sandbox: https://codesandbox.io/p/sandbox/friendly-lake-b15kkz
Hope this helps Running more than one function in Node.js

huangapple
  • 本文由 发表于 2023年4月11日 04:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定