Skip to content

Instantly share code, notes, and snippets.

@jomido
Created January 31, 2018 20:46
Show Gist options
  • Save jomido/64c089dfa9da8aa52ab4c9384bc91548 to your computer and use it in GitHub Desktop.
Save jomido/64c089dfa9da8aa52ab4c9384bc91548 to your computer and use it in GitHub Desktop.
Node Blocking Readline
// 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