Created
November 13, 2018 04:39
-
-
Save akirattii/dfff355957d1459691efd52450ffd784 to your computer and use it in GitHub Desktop.
NodeJS v10: An example of Async-Iterator with Stream API
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
| 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