DEV Community

Tevfik Tümer
Tevfik Tümer

Posted on

Reading stdin per character in NodeJS (asyncConfirm)

I bumped into this problem while I was writing a simple confirmation function. Looking to the documentation, I could not find any helper function that allows me to read only a single character from stdin. Therefore, I build a simple asyncConfirm function that allows user to confirm based on user input.

Here is the gist that contains the complete very basic example and a little description can be found below the example.

Basic idea behind my approach reading stdin with a specific limit(3 bytes) of buffer using Promises to allow async functionality. Based on giving yesOptions returns boolean false or true.

The example is tested only in unix platforms. You might need to tweak it for other platforms.

Top comments (0)