Node.js fast-csv synchronously read file and return array

34
November 24, 2020, at 7:50 PM

I am trying to read a .csv file and to assign it to an array with this code:

const fs = require("fs");
const csv = require("@fast-csv/parse");
let data = []
csv
  .parseFile("./downloads/aggiornamento.csv", { skipRows: 2 })
  .on("error", (error) => console.error(error))
  .on("data", (row) => {
    let obj = {};
    obj["item_id"] = row[2];
    obj["qty"] = row[20];
    data.push(obj);
    console.log(obj);
  })
  .on("end", function () {
    return data;
  });

However, the file read by fast-csv is a stream, hence I can only process row by row. I need the full array in a second moment to call some API service, hence I would like to read the file synchronously, appending very row, and returning the array at the end, so that, in a second moment, I can wrap the previous code in a function, and obtain the array in a way such as:

const data = readCSVSynchronously()

How can I do it with fast-csv?

Answer 1

You can't turn an asynchronous function into a synchronous one.

You could, however, wrap that eventful reading code into a promise, which you can then use in a promise/async context, like so:

const fs = require("fs");
const csv = require("@fast-csv/parse");
function readCsv(path, options, rowProcessor) {
  return new Promise((resolve, reject) => {
    const data = [];
    csv
      .parseFile(path, options)
      .on("error", reject)
      .on("data", (row) => {
        const obj = rowProcessor(row);
        if (obj) data.push(obj);
      })
      .on("end", () => {
        resolve(data);
      });
  });
}
async function doThings() {
  const data = await readCsv(
    "./downloads/aggiornamento.csv",
    { skipRows: 2 },
    (row) => ({ item_id: row[2], qty: row[20] }),
  );
  // use data in API...
}
Answer 2

Callback functions can be wrapped with promise.

const fs = require("fs");
const csv = require("@fast-csv/parse");
let data = []
let readCSVSynchronously = async () = {
    return new Promise(reject, resolve => {
     const results = [];
     csv
      .parseFile("./downloads/aggiornamento.csv", { skipRows: 2 })
      .on("error", (error) => console.error(error)
          return reject(error);
       )
      .on("data", (row) => {
        // Do your logic
        results.push(row);
      })
      .on("end", function () {
          resolve(results)
      });
    });
}
const totalResults = await readCSVSynchronously();
READ ALSO
I want to upload a file to file.cm from telegram

I want to upload a file to file.cm from telegram

I want to upload a file to filecm from telegram, but I could not find any case studies on this topic

36
how do i solve this mistake i'm going crazy

how do i solve this mistake i'm going crazy

enter image description here

46
Inserting HTML snippet into an HTML file using node.js

Inserting HTML snippet into an HTML file using node.js

I'm doing some webscraping using puppeteer and want to store the HTML snippets I scrape inside an existing file

46
Unable to set value using useState()

Unable to set value using useState()

I'm trying to set value that is being imported from another functionBut when I try to set it using useState() it returns empty array

18