rl.question(query[, options], callback)


  • query <string> 要写入 output 的语句或查询,位于提示之前。
  • options <Object>
    • signal <AbortSignal> 可选择允许使用 AbortController 取消 question()
  • callback <Function> 使用用户输入调用的回调函数以响应 query

rl.question() 方法通过将 query 写入 output 来显示 query,等待在 input 上提供用户输入,然后调用 callback 函数,将提供的输入作为第一个参数传入。

当调用时,如果 rl.question() 流已暂停,则 rl.question() 将恢复 input 流。

如果 readline.Interface 是在 output 设置为 nullundefined 的情况下创建的,则不会写入 query

传给 rl.question()callback 函数不遵循接受 Error 对象或 null 作为第一个参数的典型模式。 以提供的答案作为唯一参数调用 callback

用法示例:

rl.question('What is your favorite food? ', (answer) => {
  console.log(`Oh, so your favorite food is ${answer}`);
});

使用 AbortController 取消问题。

const ac = new AbortController();
const signal = ac.signal;

rl.question('What is your favorite food? ', { signal }, (answer) => {
  console.log(`Oh, so your favorite food is ${answer}`);
});

signal.addEventListener('abort', () => {
  console.log('The food question timed out');
}, { once: true });

setTimeout(() => ac.abort(), 10000);

如果此方法被调用为 util.promisify() 的版本,则它会返回使用答案履行的 Promise。 如果使用 AbortController 取消问题,则它将使用 AbortError 拒绝。

const util = require('node:util');
const question = util.promisify(rl.question).bind(rl);

async function questionExample() {
  try {
    const answer = await question('What is you favorite food? ');
    console.log(`Oh, so your favorite food is ${answer}`);
  } catch (err) {
    console.error('Question rejected', err);
  }
}
questionExample();
  • query <string> A statement or query to write to output, prepended to the prompt.
  • options <Object>
    • signal <AbortSignal> Optionally allows the question() to be canceled using an AbortController.
  • callback <Function> A callback function that is invoked with the user's input in response to the query.

The rl.question() method displays the query by writing it to the output, waits for user input to be provided on input, then invokes the callback function passing the provided input as the first argument.

When called, rl.question() will resume the input stream if it has been paused.

If the readline.Interface was created with output set to null or undefined the query is not written.

The callback function passed to rl.question() does not follow the typical pattern of accepting an Error object or null as the first argument. The callback is called with the provided answer as the only argument.

Example usage:

rl.question('What is your favorite food? ', (answer) => {
  console.log(`Oh, so your favorite food is ${answer}`);
});

Using an AbortController to cancel a question.

const ac = new AbortController();
const signal = ac.signal;

rl.question('What is your favorite food? ', { signal }, (answer) => {
  console.log(`Oh, so your favorite food is ${answer}`);
});

signal.addEventListener('abort', () => {
  console.log('The food question timed out');
}, { once: true });

setTimeout(() => ac.abort(), 10000);

If this method is invoked as it's util.promisify()ed version, it returns a Promise that fulfills with the answer. If the question is canceled using an AbortController it will reject with an AbortError.

const util = require('node:util');
const question = util.promisify(rl.question).bind(rl);

async function questionExample() {
  try {
    const answer = await question('What is you favorite food? ');
    console.log(`Oh, so your favorite food is ${answer}`);
  } catch (err) {
    console.error('Question rejected', err);
  }
}
questionExample();