第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 readline 在 javascript 中輸入的一系列用戶輸入

使用 readline 在 javascript 中輸入的一系列用戶輸入

牛魔王的故事 2021-08-20 10:30:49
我在 array 中存儲了幾個問題var questions=[]。我正在使用forEach遍歷數(shù)組并為每個問題輸入輸入并將其顯示在終端本身上。但它只詢問第一個問題,顯示響應(yīng)并停留在那里。它不會轉(zhuǎn)移到下一個問題。我應(yīng)該使用rl.close(),但在哪里。這是我的代碼Quiz.js。const readline = require('readline');const rl = readline.createInterface({  input: process.stdin,  output: process.stdout});var questions=[  "Hi, What is your name?",  "I need your contact number also",  "Thanks! What is your favourite color?"];questions.forEach(myFunction);function myFunction(item, index) {  rl.question(item, (answer) => {    console.log(`You said: ${answer}`);  });  rl.close(); //THIS IS IMMEDIATELY CLOSING AFTER THE FIRST QUESTION}請糾正我。
查看完整描述

2 回答

?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個贊

我在 array 中存儲了幾個問題var questions=[]。我正在使用forEach遍歷數(shù)組并為每個問題輸入輸入并將其顯示在終端本身上。但它只詢問第一個問題,顯示響應(yīng)并停留在那里。它不會轉(zhuǎn)移到下一個問題。我應(yīng)該使用rl.close(),但在哪里。這是我的代碼Quiz.js。


const readline = require('readline');


const rl = readline.createInterface({

  input: process.stdin,

  output: process.stdout

});


var questions=[

  "Hi, What is your name?",

  "I need your contact number also",

  "Thanks! What is your favourite color?"

];


questions.forEach(myFunction);


function myFunction(item, index) {

  rl.question(item, (answer) => {

    console.log(`You said: ${answer}`);

  });

  rl.close(); //THIS IS IMMEDIATELY CLOSING AFTER THE FIRST QUESTION

}

請糾正我。


查看完整回答
反對 回復(fù) 2021-08-20
?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個贊

問完所有問題后,您是否嘗試關(guān)閉它?


我的意思是:


function myFunction(item, index) {

  rl.question(item, (answer) => {

    console.log(`You said: ${answer}`);

  });


  if (index === questions.length - 1) {

     rl.close();

  }

}

=== === ===


如果它仍然無法正常工作,請嘗試以下操作:


const readline = require('readline');


const rl = readline.createInterface({

  input: process.stdin,

  output: process.stdout,

});


var questions = [

  'Hi, What is your name?',

  'I need your contact number also',

  'Thanks! What is your favourite color?',

];


const ask = (question) => {

  return new Promise(resolve => rl.question(question, resolve))

}


const askAll = async (questions) => {

  const answers = []


  for (let q of questions) {

    answers.push(await ask(q))

    console.log('You said:', answers[answers.length - 1]);

  }


  return answers

}


askAll(questions).then(rl.close)

我關(guān)閉了函數(shù)的rl外部askAll,因?yàn)槲艺J(rèn)為函數(shù)最好不要知道資源管理之類的東西。


查看完整回答
反對 回復(fù) 2021-08-20
  • 2 回答
  • 0 關(guān)注
  • 402 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號