Created
August 15, 2017 21:49
-
-
Save rauschma/d75f7352eff110a3a0828959213f9869 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
/** | |
* Creates an asynchronous ReadStream for the file whose name | |
* is `fileName` and feeds it into an AsyncQueue that it returns. | |
* | |
* @returns an async iterable | |
*/ | |
function readFile(fileName) { | |
const queue = new AsyncQueue(); | |
const readStream = createReadStream(fileName, | |
{ encoding: 'utf8', bufferSize: 1024 }); | |
readStream.on('data', buffer => { | |
const str = buffer.toString('utf8'); | |
queue.enqueue(str); | |
}); | |
readStream.on('end', () => { | |
// Signal end of output sequence | |
queue.close(); | |
}); | |
return queue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment