Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: incorrect readline.question promisify example #38039

Closed
1 task done
accetone opened this issue Apr 2, 2021 · 1 comment
Closed
1 task done

doc: incorrect readline.question promisify example #38039

accetone opened this issue Apr 2, 2021 · 1 comment
Labels
doc Issues and PRs related to the documentations.

Comments

@accetone
Copy link

accetone commented Apr 2, 2021

📗 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.
@accetone accetone added the doc Issues and PRs related to the documentations. label Apr 2, 2021
@Ayase-252
Copy link
Member

Ayase-252 commented Apr 2, 2021

From the utils docs

If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.

In the case, readline.question did implement promisify.custom property here,

Interface.prototype.question[promisify.custom] = function(query, options) {

therefore, the custom will be used here. IMO, the example in question is valid.

@accetone accetone closed this as completed Apr 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations.
Projects
None yet
Development

No branches or pull requests

2 participants