Created
September 20, 2020 00:23
-
-
Save shizuka-na-kazushi/1f03e4a5739ab36d0c2531973f07a21d to your computer and use it in GitHub Desktop.
Readable streamを使って、「何か」をする関数の本体
This file contains 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
// 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