Skip to main content
Go to Latest
method Interface.prototype.question
Re-export
import { Interface } from "https://deno.land/std@0.147.0/node/readline.ts";

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 callbackfunction 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 orundefined 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('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();

Parameters

query: string

A statement or query to write to output, prepended to the prompt.

callback: (answer: string) => void

A callback function that is invoked with the user's input in response to the query.

Parameters

query: string
options: Abortable
callback: (answer: string) => void