Skip to content

Instantly share code, notes, and snippets.

@melnikaite
Created April 7, 2021 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melnikaite/01c13f5f784fb3d708135fe5e5ab375f to your computer and use it in GitHub Desktop.
Save melnikaite/01c13f5f784fb3d708135fe5e5ab375f to your computer and use it in GitHub Desktop.
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