Skip to content

doc: incorrect readline.question promisify example  #38039

@accetone

Description

@accetone

📗 API Reference Docs Problem

  • Version: 15.13.0

  • Platform: Windows 10 x64

  • Subsystem: readline

Location

Section of the site where the content exists

Affected URL(s):

Description

Concise explanation of the problem

Here is a suggested code sample from docs that should allow to use question method with Promise-based API:

const readline = require('readline');
const util = require('util');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

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();

The problem with code above is that it will go into catch section on user input. Cause of this is that question method callback accept user answer as first parameter which conflict with node-style callback convention used by util.promisify.

So readline.question can't be used alongside with util.promisify. I see two possible solutions: a) remove this section from docs or b) provide code snippet that shows how to correctly wrap this method with Promise. If you interested in correct code snippet I can work on draft.


  • I would like to work on this issue and
    submit a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions