Last active
September 16, 2019 14:23
-
-
Save lacti/1cbd75ba316e73c7dd76f8bd255a10c6 to your computer and use it in GitHub Desktop.
The `readline.close` magic from NodeJS + Windows
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
| 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