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
"Keypress" not working when we await answer to a "question" placed in a promise #42800
Hello, I have to use keypress after asking a question to a user. For those 2 actions I use readline as follow :
importreadlinefrom"readline";constmenu=readline.createInterface({input: process.stdin,output: process.stdout});letanswer=0;constmyFunc=async()=>{returnnewPromise((resolve,reject)=>{menu.question('Choose an exchange to use : ',function(answer){if(isNaN(answer)||parseInt(answer)<1||parseInt(answer)>10)answer=undefined;menu.close();resolve(answer);});});}answer=awaitmyFunc();readline.emitKeypressEvents(process.stdin)process.stdin.setRawMode(true);process.stdin.on('keypress',async(character,key)=>{if(key.name==='c'&&key.ctrl){process.exit()}if(key.name==='1'||key.name==='2'||key.name==='3'||key.name==='4'){console.log(key.name)}});
But I have notice that keypress event is definitely not working when I ask a question before. If I just remove the question part, it works as expected.
How often does it reproduce? Is there a required condition?
It always happens when the condition of asking a question before is respected. I have noticed that when I remove "await" before the function call, it works as expected
What is the expected behavior?
The expected behaviour is : program print "1", "2", "3", "4", when I hit one of these keys, or exit when I hit ctrl+c
changed the title [-]"Keypress" not working after using "question"[/-][+]"Keypress" not working when we await answer to a "question" placed in a promise [/+]on Apr 20, 2022
The await myFunc is blocking the rest of the code to be executed, which results on the 'keypress' event listener to be added only after the 'question' event has been emitted. If you move the answer = await myFunc() after the process.stdin.on('keypress', …) call, it should work as expected.
FWIW, you can use node:readline/promises module on Node.js 17+:
import{createInterface}from'node:readline/promises';import{emitKeypressEvents}from'node:readline';constmenu=createInterface({input: process.stdin,output: process.stdout,});letanswer=0;emitKeypressEvents(process.stdin);process.stdin.setRawMode(true);process.stdin.on('keypress',async(character,key)=>{switch(key.name){case'1':
case'2':
case'3':
case'4':
console.log(key.name);break;case'c':
if(key.ctrl)process.exit();default:
}});answer=awaitmenu.question('Choose an exchange to use : ');if(isNaN(answer)||parseInt(answer)<1||parseInt(answer)>10)answer=undefined;menu.close();
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.
Activity
[-]Keypress not working after using createInterface[/-][+]"Keypress" not working after using "question"[/+][-]"Keypress" not working after using "question"[/-][+]"Keypress" not working when we await answer to a "question" placed in a promise [/+]aduh95 commentedon Apr 20, 2022
The
await myFuncis blocking the rest of the code to be executed, which results on the'keypress'event listener to be added only after the'question'event has been emitted. If you move theanswer = await myFunc()after theprocess.stdin.on('keypress', …)call, it should work as expected.FWIW, you can use
node:readline/promisesmodule on Node.js 17+:github-actions commentedon Jun 25, 2026
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
github-actions commentedon Jul 27, 2026
This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.