简体   繁体   中英

How to read line from console synchronously in ts-node

I am working on a node.js application which shows some menu options on console and asks for input (1-5). Application is in tyepscript .

So I can print my menu using console.log() .

console.log('1: Option#1');
console.log('2: Option#2');
console.log('3: Option#3');
console.log('4: Option#4');

// some way to read input from console
var opt = readFromConsole();  // HOW TO READ?

// perform some action on the basis of those options
someAction();

But how to read from console synchronously in tyepscript ?

I found one node.js module readline-sync . But it seems to work of node.js applications. My application is totally typescript based. So is there any typescript module which I can use to fullfill my purpose? Or, in any way can I use fs for my purpose?

Any kind of help/suggestion is appreciated.

read from console synchronously

You can make async code -> sync with a careful while loop + setTimeout, but I would advise against that. Instead just use the built in async readline : https://nodejs.org/api/readline.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM