title: Read and write to csv file with Node.js fast-csv library
body: <p>I may be lacking some in depth understanding of streams in general. However, I would like to know how efficiently what I need should work.</p>
  
  <p>I want to implement so that a csv file would be read, then to each row a query to the database (or api) is made and data is attached. After that the row with attached data is written to a new csv file. I am using fast-csv node library for this.</p>
  
  <p>Here is my implementation:</p>
  
  <pre><code>const fs = require("fs");
  const csv = require("fast-csv");
  const delay = t =&gt; new Promise(resolve =&gt; setTimeout(resolve, t));
  
  
  const asyncFunction = async (row, csvStream) =&gt; {
    // Imitate some stuff with database
    await delay(1200);
    row.data = "data";
    csvStream.write(row);
  };
  
  const array = [];
  
  const csvStream = csv.format({ headers: true });
  
  const writeStream = fs.createWriteStream("output.csv");
  
  csvStream.pipe(writeStream).on("finish", () =&gt; {
    console.log("End of writing");
  });
  fs.createReadStream("input.csv")
    .pipe(csv.parse({ headers: true }))
    .transform(async function(row, next) {
      array.push(asyncFunction(row, csvStream));
      next();
    })
    .on("finish", function() {
      console.log("finished reading file");
    //Wait for all database requests and writings to be finished to close write stream
      Promise.all(array).then(() =&gt; {
        csvStream.end();
        console.log("finished writing file");
      });
    });
  </code></pre>
  
  <p>Particularly I would like to know are there ways to optimize what I am doing here, because I feel that I am missing something important on how this library can be used for these type of cases</p>
  
  <p>Regards,
  Rokas</p>