Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

434
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda