Skip to content

Instantly share code, notes, and snippets.

@lacti
Last active September 16, 2019 14:23
Show Gist options
  • Select an option

  • Save lacti/1cbd75ba316e73c7dd76f8bd255a10c6 to your computer and use it in GitHub Desktop.

Select an option

Save lacti/1cbd75ba316e73c7dd76f8bd255a10c6 to your computer and use it in GitHub Desktop.
The `readline.close` magic from NodeJS + Windows
const readline = require("readline");
// This promise cannot be resolved with high probability if a millis is bigger than 1.
const next = () => new Promise(fulfill => setTimeout(fulfill, 2 /* MAGIC */));
const rl = new readline.createInterface({
input: process.stdin,
output: process.stdout,
// But everything is ok if you set `terminal` to `false.
// terminal: false,
});
rl.on('close', () => console.log(`rl is closed!`));
rl.on('resume', () => console.log(`rl is resumed!`));
// This would be stuck, too!
setInterval(() => console.log(`tick!`), 2);
(async () => {
console.log(`now, close rl`);
await next();
rl.close();
console.log(`this would be called`);
await next();
console.log(`this wouldn't be called`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment