You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
doc: incorrect readline.question promisify example #38039
Here is a suggested code sample from docs that should allow to use question method with Promise-based API:
constreadline=require('readline');constutil=require('util');constrl=readline.createInterface({input: process.stdin,output: process.stdout,});constquestion=util.promisify(rl.question).bind(rl);asyncfunctionquestionExample(){try{constanswer=awaitquestion('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.
Activity
Ayase-252 commentedon Apr 2, 2021
From the utils docs
In the case,
readline.questiondid implementpromisify.customproperty here,node/lib/readline.js
Line 418 in 1bead01
therefore, the custom will be used here. IMO, the example in question is valid.