Created
April 7, 2021 18:09
-
-
Save melnikaite/01c13f5f784fb3d708135fe5e5ab375f to your computer and use it in GitHub Desktop.
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 { Readable } = require('stream'); | |
class RandomNumberStream extends Readable { | |
constructor(options) { | |
super(options); | |
this.isTimeToEndIt = false; | |
setTimeout(() => { | |
this.isTimeToEndIt = true; | |
}, 10 * 1000); | |
} | |
async _read() { | |
await new Promise(resolve => { | |
setTimeout(resolve, 1000); | |
}); | |
if (this.isTimeToEndIt) { | |
this.push(null); | |
return; | |
} | |
const randomNumberString = String(Math.floor(Math.random() * 10 + 1)); | |
this.push(`${randomNumberString}\n`); | |
} | |
} | |
const randomNumbers = new RandomNumberStream(); | |
randomNumbers.pipe(process.stdout); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment