I'm trying to:
- write a simple awaitable async function for a TUI node.js app
- that waits for a single key press on the keyboard (without having to press Enter after)
- and returns info about the key that was pressed
- ...without using any NPM packages
Here's my function:
export async function inkey_promise() {
console.log('Press any key...');
return new Promise((resolve) => {
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
const listener = (str, readline_key) => {
process.stdin.setRawMode(false);
process.stdin.resume();
console.log(`RETURNING: `, readline_key);
return resolve(readline_key);
};
process.stdin.once('keypress', listener);
});
}
And I call it like:
const key_info = await inkey_promise();
Problem:
After pressing a key:
- As expected:
- The promise resolves
- And my code after the await carries on
- But:
- When there's nothing left to do, my node.js app remains running forever, it never exits unless I kill the process manually
- I don't want to explicitly do a
process.exit()here, because it will always be doing stuff after the key press... but when there's nothing left for it to do, it should still exit normally as it used to.
So it seems that there's something else I need to do to fully return the process/stdin/keyboard back to its default behaviour/state?
rl.inputrl['input']instead.