Skip to content

Commit

Permalink
docs(csv-stringify): iterator sample
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 1, 2023
1 parent 71c36bd commit 29d6b6e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/csv-stringify/samples/api.async.iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import assert from 'assert';
import { generate } from 'csv-generate';
import { stringify } from 'csv-stringify';

(async () => {
// Initialise the parser by generating random records
const parser = generate({
length: 1000,
objectMode: true,
seed: true
}).pipe(
stringify()
);
// Intialise count
let count = 0;
// Report start
process.stdout.write('start\n');
// Iterate through each records
for await (const row of parser) {
// Report current line
process.stdout.write(`${count++} ${row}\n`);
// Fake asynchronous operation
await new Promise((resolve) => setTimeout(resolve, 100));
}
// Report end
process.stdout.write('...done\n');
// Validation
assert.strictEqual(count, 5);
})();

0 comments on commit 29d6b6e

Please sign in to comment.