Readline挂在terminal的第一个input

使用节点版本8.1.0。

阅读readline上的文档: https : //nodejs.org/docs/latest-v8.x/api/readline.html

说明密切的方法,它指出:

readline.Interface实例应该被认为是“完成”,一旦“closures”事件发射。

所以自然我会打开一个新的界面,就像我在我的例子中所做的那样。

const readline = require('readline'); function question(question, defaultAnswer) { // Create the interface const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); return new Promise((resolve, reject) => { rl.question(question, (answer) => { // Relinquished control over the input and output streams rl.close(); if (answer.length === 0) { resolve(defaultAnswer); } else { resolve(answer); } }); }); } (async () => { let answer; answer = await question('1? [y] ', 'y'); console.log('answer', answer); answer = await question('2? [y] ', 'y'); console.log('answer', answer); answer = await question('3? [y] ', 'y'); console.log('answer', answer); })(); 

输出:

 $ node test.js 1? [y] y answer y 2? [y] 

它挂起然而。 我不知道为什么? 目前坐在一台Mac上不幸的是,我不知道这是否会影响到这一点。

所以我在这里提交了一个bug报告: https : //github.com/nodejs/node/issues/17495

这是节点8.1.0的已知问题。 从8.1.0更改为任何其他版本使其工作。