Skip to content

Instantly share code, notes, and snippets.

@shizuka-na-kazushi
Created September 20, 2020 00:23
Show Gist options
  • Save shizuka-na-kazushi/1f03e4a5739ab36d0c2531973f07a21d to your computer and use it in GitHub Desktop.
Save shizuka-na-kazushi/1f03e4a5739ab36d0c2531973f07a21d to your computer and use it in GitHub Desktop.
Readable streamを使って、「何か」をする関数の本体
// readableを使って「何か」をする関数の本体
module.exports = function doSomethingWithTheStream(readable) {
return new Promise(function(resolve, reject){
var count = 0;
readable.on('readable', () => {
// 読み取り
var data;
while (data = readable.read()) {
// ここで読み取った部分的なデータ(chunk)を処理する...
console.log(`do something using read data from stream: cycle=${count++}`);
}
});
readable.on('end', () => {
// streamの最後に到達したら、resolveで処理を完了する
resolve();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment