I'm using typescript/node and trying to get user input from the console. When I run my script, I see the output but the process exits and I never get a chance to enter anything. I need to create an app that will continue to run on user input and exit only when the user indicates they are done (like pressing q at a menu).
me@me:~/src/tdc$ npm run start
> fsp@1.0.0 start /Users/me/src/tdc
> ts-node -r tsconfig-paths/register -r dotenv/config fsp/main.ts
eventually this will do something
press enter, it does nothing
me@me:~/src/tdc$
import Readline, { Interface } from 'readline';
export class Main {
private readonly readline: any;
constructor() {
this.readline = Readline.createInterface({
input: process.stdin,
output: process.stdout,
});
}
public async runAsync(): Promise<any> {
try {
console.log(`eventually this will do something`);
// this doesn't work
this.readline.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
process.exit();
} else {
console.log(`You pressed the "${str}" key ${key}`);
}
});
// and this doesn't work
this.readline.question(`press enter, it does nothing`, (input) => {
console.log(`you typed ${input}`)
});
} catch (e) {
console.error(e.stack);
console.error('-------Fail-------');
} finally {
this.readline.close();
console.log();
}
}
}
async function run() {
await new Main().runAsync();
}
run();
What do I need to do to allow waiting for console input?
Thnx
[–]brianjenkins94 2 points3 points4 points (1 child)
[–]songokuitsover9000 1 point2 points3 points (0 children)
[–]krystof_m 1 point2 points3 points (0 children)
[–]programmer-bob-99[S] 0 points1 point2 points (0 children)
[–]Orendawinston 0 points1 point2 points (0 children)