Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

432
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda