Async iterator API

The Async iterator API is both scalable and elegant. It takes advantage of the native Readable Stream API upon which the parser is build to iterate over the parsed records.

The async iterator example below generate a CSV stream which is then parsed and iterated. For each record, we simulate a slow asynchronous operation. This example is available with the command node samples/async.iterator.js.

const assert = require('assert');
const generate = require('csv-generate');
const parse = require('..');

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