Skip to content

Instantly share code, notes, and snippets.

@akirattii
Created November 13, 2018 04:39
Show Gist options
  • Save akirattii/dfff355957d1459691efd52450ffd784 to your computer and use it in GitHub Desktop.
Save akirattii/dfff355957d1459691efd52450ffd784 to your computer and use it in GitHub Desktop.
NodeJS v10: An example of Async-Iterator with Stream API
const fs = require("fs");
const inputFilePath = process.argv[2];
console.log("file:", inputFilePath);
async function main(inputFilePath) {
const readStream = fs.createReadStream(inputFilePath, { encoding: 'utf8', highWaterMark: 1024 });
// NOTE: Async-Iterator starts with NodeJS v10, but it's experimental yet. (Current latest LTS version: v10.13.0)
for await (const chunk of readStream) {
console.log('>>> '+chunk);
}
console.log('### DONE!');
}
main(inputFilePath).catch(console.error);
/* log:
$ node async-iterator-example.js "./hoge.csv"
file: ./hoge.csv
(node:15500) ExperimentalWarning: Readable[Symbol.asyncIterator] is an experimental feature. This feature could change at any time
>>> aaaa,bbbb,ccc
1111,222,33333333
xxxx,yyyy,zzzzzzzzzzzzzzzz
### DONE!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment