Created
January 31, 2018 20:46
-
-
Save jomido/64c089dfa9da8aa52ab4c9384bc91548 to your computer and use it in GitHub Desktop.
Node Blocking Readline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // THIS WILL NOT WORK UNTIL NODE implements `Atomics` :) Soon? | |
| const readline = require('readline'); | |
| const rl = readline.createInterface({ | |
| input: process.stdin, | |
| output: process.stdout | |
| }); | |
| function msleep(n) { | |
| Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, n); | |
| } | |
| function sleep(n) { | |
| msleep(n * 1000); | |
| } | |
| const prompt = (p): string => { | |
| let answer = null | |
| rl.question(p, (a) => { | |
| answer = a | |
| rl.close(); | |
| }); | |
| while (answer === null) { | |
| sleep(10) | |
| } | |
| return answer || '' | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment