The Async iterator API is both scalable and elegant. It takes advantage of the native Readable Stream API upon which the parser is built to iterate over the parsed records.
The async iterator example below generates a CSV stream which is then parsed and iterated over. For each record, we simulate a slow asynchronous operation. This example is available with the command node samples/async.iterator.js.
import assert from'node:assert';import{ generate }from'csv-generate';import{ parse }from'csv-parse';(async()=>{// Initialise the parser by generating random recordsconst parser =generate({high_water_mark:64*64,length:100}).pipe(parse());// Intialise countlet count =0;// Report start
process.stdout.write('start\n');// Iterate through each recordsforawait(const record of parser){// Report current line
process.stdout.write(`${count++}${record.join(',')}\n`);// Fake asynchronous operationawaitnewPromise((resolve)=>setTimeout(resolve,100));}// Report end
process.stdout.write('...done\n');// Validation
assert.strictEqual(count,100);})();