Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using async function to make await call #226

Open
dawadam opened this issue Mar 24, 2023 · 4 comments
Open

Using async function to make await call #226

dawadam opened this issue Mar 24, 2023 · 4 comments

Comments

@dawadam
Copy link

dawadam commented Mar 24, 2023

Feature Proposal

Using async function to make await call.

Feature Use Case

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

fs.createReadStream('file.csv')
  .pipe(csv())
  .on('data', async (data) => {
    await myAsyncFunction(data);
  })
  .on('end', async () => {
    await myAsyncFunctionEndOfRead();
  });
@dawadam
Copy link
Author

dawadam commented Mar 24, 2023

Alternative can be (thanks ChatGPT) :

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

async function processLine(line) {
  return new Promise((resolve, reject) => {
    // Votre traitement asynchrone des données de la ligne
    setTimeout(() => {
      console.log(line);
      resolve();
    }, 1000);
  });
}

async function processCsvFile(filePath) {
  return new Promise((resolve, reject) => {
    const stream = fs.createReadStream(filePath).pipe(csv());
    stream.pause();
    stream.on('data', (line) => {
      stream.pause();
      processLine(line)
        .then(() => {
          stream.resume();
        })
        .catch((err) => {
          reject(err);
        });
    });
    stream.on('end', () => {
      console.log('Fin du traitement du fichier CSV.');
      resolve();
    });
    stream.on('error', reject);
    stream.resume();
  });
}

processCsvFile('path/to/file.csv')
  .then(() => {
    console.log('Traitement terminé.');
  })
  .catch((err) => {
    console.error('Une erreur est survenue :', err);
  });

@agustinv
Copy link

@dawadam you want to modify the promise to:

processLine(line)
        .then(() => {
           if(stream.readableEnded) {
                console.log('Fin du traitement du fichier CSV.')
                resolve();
              } else {
                stream.resume();
              } 
        })
        .catch((err) => {
          reject(err);
        });

Otherwise you return before the last row's promise resolves!

@ChuckJonas
Copy link

@markg85
Copy link

markg85 commented Feb 13, 2025

I have spend SO MUCH TIME getting this to work and pulling my hair out when it didn't work.. Then finding this thread with the (AI geerated code.. for f**** sake). Yeah, now it works.

Another place said that csv-parser is a bit naughty here causing the whole issue to begin with.

Anyhow, i'm happy i found this solution. Can i have my wasted hours back now? 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants