Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created August 15, 2017 21:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rauschma/d75f7352eff110a3a0828959213f9869 to your computer and use it in GitHub Desktop.
Save rauschma/d75f7352eff110a3a0828959213f9869 to your computer and use it in GitHub Desktop.
/**
* 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