gpt4 book ai didi

node.js - 在终端中首次输入后 Readline 挂起

转载 作者:太空宇宙 更新时间:2023-11-04 00:14:45 24 4
gpt4 key购买 nike

使用 Node 版本 8.1.0。

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

声明 close 方法,它声明:

The readline.Interface instance should be considered to be "finished" once the 'close' event is emitted.

因此,我很自然地会打开一个新界面,就像我在示例中所做的那样。

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,我不知道这是否会影响这一点。

最佳答案

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

这是 Node 8.1.0 的一个已知问题。从 8.1.0 更改为任何其他版本都可以正常工作。

关于node.js - 在终端中首次输入后 Readline 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47675534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com