0

I am using Papaparse to parse csv files. I have a file which has about 10k records in it. My aim is to read each row and perform some API call and update data on that row and write the updated row in new file.

return new Promise((resolve, reject) => {

  async function transformData(data) {
    ...
    making some api calls here
    ...
  }

  const readStream = fs.createReadStream(self.INPUT_FILE_NAME, { encoding: "utf8" });

  const writeStream = fs.createWriteStream(self.OUTPUT_FILE_NAME, { encoding: "utf8" });

  writeStream.on("drain", () => {
        readStream.resume()
  })

      papa.parse(readStream, {
        header: true,
        worker: true,
        skipEmptyLines: true,
        encoding: "utf8",
        step: async function (result, file) {
           const data = result.data;
           const transformedData = await transformData(data)
           const isWriteCompleted = csvStream.write(transformedData);
           if (!isWriteCompleted) {
             readStream.pause()
           }
        },
        complete: function (results, file) {
          // csvStream.end() // cannot end because write stores data in buffer
          logger.info("CSV file modified and saved successfully!");
          readStream.close()
          resolve("Success");
        },
        error: function (error) {
          csvStream.end()
          logger.error("Error writing CSV file:", error);
          reject(error);
        },
      })
})

Since there was some issue with backpressure I am pausing my readStream. The problem I am having is the step function does not wait for transformData to finish, it just keeps of reading rows from csv and once all rows are read it calls the complete function. And inside complete function I have resolved the promise so code returns to the caller and I am not able to completely write all records that have been read and also I don't know when should I close my writeStream.

Been struggling with it for quite a bit time now. I don't really understand how to solve this problem.

My question is similar to this question

Thank you for any kind of help.

Shailesh B
  • 141
  • 12

0 Answers0