Skip to content

Commit cd3866a

Browse files
committed
docs: add async iterables example
1 parent 30f12bf commit cd3866a

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ Bugs Bunny,22
9292
It could then be parsed, and results shown like so:
9393

9494
``` js
95-
import csv from 'csv-parser';
9695
import fs from 'node:fs';
96+
import csv from 'csv-parser';
9797

9898
const results = [];
9999

@@ -109,6 +109,27 @@ fs.createReadStream('data.csv')
109109
});
110110
```
111111

112+
You can also use async iterators in modern Node.js versions:
113+
114+
```js
115+
import fs from 'node:fs';
116+
import csv from 'csv-parser';
117+
118+
async function readCSV(file) {
119+
const results = [];
120+
121+
for await (const data of fs.createReadStream(file).pipe(csv())) {
122+
results.push(data);
123+
}
124+
125+
return results;
126+
}
127+
128+
readCSV('data.csv')
129+
.then((results) => console.log(results))
130+
.catch((err) => console.error(err));
131+
```
132+
112133
To specify options for `csv`, pass an object argument to the function. For
113134
example:
114135

0 commit comments

Comments
 (0)