The Async iterator API is both scalable and elegant. It takes advantage of the native Readable Stream API upon which the stringifier is build to iterate over the stringified chunks of data.
The async iterator example below generates a CSV stream which is then stringified and iterated over. For each record, we simulate a slow asynchronous operation. This example is available with the command node samples/api.async.iterator.js.
import assert from'node:assert';import{ generate }from"csv-generate";import{ stringify }from"csv-stringify";(async()=>{// Initialise the parser by generating random recordsconst stringifier =generate({length:1000,objectMode:true,seed:true,}).pipe(stringify());// Count recordslet count =0;// Report start
process.stdout.write("start...\n");// Iterate through each recordsforawait(const row of stringifier){// Report current line
process.stdout.write(`${count++}${row}\n`);// Fake asynchronous operationawaitnewPromise((resolve)=>setTimeout(resolve,100));}// Report end
process.stdout.write("...done\n");// Validation
assert.strictEqual(count,6);})();