Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

423
Views
can't resolve a promise of a readstream ('csv-parser')

I am trying to parse data from a .csv file, and save it to an array for later use. I understand the concept of promises, but I have no idea what am I missing in my code that I cannot resolve the Promise and get the value (the string in the .csv file). It while I can view all the data inside the promise (.on('data')) from debugging mode, I just can't save it in order to use it later in my 'try&catch'.

const fs = require("fs");
const csv = require("csv-parser");
const { resolve } = require("path");

async function readCSV(filepath) {
  return new Promise(async (resolve, reject) => {
    await fs
      .createReadStream(filepath)
      .pipe(csv())
      .on("data", (data) => {
        results.push(data);
      })
      .on("error", (error) => reject(results))
      .on("end", () => {
        resolve(results);
      });
  });
}


const results = [];
const csvFilePath =
  "/languages.csv";

try {
  const languages = readCSV(csvFilePath).then((res) => {
    return res;
  });

  console.log(languages);

} catch (e) {
  console.log(e);
}

and the output on the console is:

>Promise {<pending>}
No debugger available, can not send 'variables'

** That's from the debugging mode when I pause inside the promise:

https://i.stack.imgur.com/H9nHi.png

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't try catch a returned promise without the await keyword in an async function.

If you're returning a promise, you need to use the .catch method on the promise.

Also, when you're logging languages you're doing so before the promise resolves because you're not using the await keyword.

I'm sure the promise resolves. Instead, log res inside the .then method.

const fs = require("fs");
const csv = require("csv-parser");

const results = [];

function readCSV(filepath) {
  return new Promise((resolve, reject) => {
    fs
      .createReadStream(filepath)
      .pipe(csv())
      .on("data", (data) => {
        results.push(data);
      })
      .on("error", (error) => reject(results))
      .on("end", () => {
        resolve(results);
      });
  });
}


const csvFilePath = "./languages.csv";

(async () => {
  const output = await readCSV(csvFilePath);
  console.log(output)
})();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!