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

118
Views
How to avoid for await...of with csv-parse

I am looking to be educated on this issue as I have spent a few days trying to resolve it on my own, to no avail.

I am using csv-parse to parse a CSV file.
I am using ESLint as my Linter
I am using the Airbnb JavaScript Style Guide plugin for ESLint
I am running this on the backend using NodeJS

My function is:

const { parse } = require('csv-parse');
const fs = require('fs');
const csvFile = 'myCsvFile.csv';

async function parseCsv(csvFile) {
  const records = [];
  const parser = fs.createReadStream(csvFile).pipe(parse({ delimiter: ',', columns: true }));
  for await (const record of parser) {
    records.push(record);
  }
  return records;

The function works well, however I am trying to abide by Airbnb's Style Guide, which does not like the for await...of loop as it is hitting me with the no-restricted-syntax violation.

I am curious on if there is a better way to write this to fall in line with Airbnb's Style Guide or, if this is one of those situations where it's OK to ignore the violation?

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

0

The styleguide says:

11.2 Don’t use generators for now. Why? They don’t transpile well to ES5.

Fortunately if you're using a recent NodeJS version, you don't need to transpile down, and can use the engine's native support. For browsers this advice is also outdated soon.

about 4 years ago · Juan Pablo Isaza Report

0

How about using events end returning promise?

const { parse } = require('csv-parse');
const fs = require('fs');
const csvFile = 'myCsvFile.csv';

async function parseCsv(csvFile) {
  return new Promise((resolve) => {
    const records = [];
    const stream = fs.createReadStream(csvFile);
    const parser = stream.pipe(parse({ delimiter: ',', columns: true }));
    
    parser.on('readable', () => {
      while (record = parser.read()) {
        records.push(record);
      }
    });

    let ended = false;
    const end = (error) => {
      if (error) {
        console.error(error.message);
      }

      if (!ended) {
        ended = true;
        resolve(records);
      }
    };

    parser.on('error', end);
    parser.on('end', end);
  });
}

also if You have node 15+ then try stream/promises example:

const { parse } = require('csv-parse');
const fs = require('fs');
const { finished } = require('stream/promises');
const csvFile = 'myCsvFile.csv';

async function parseCsv(csvFile) {
    const records = [];
    const stream = fs.createReadStream(csvFile);
    const parser = stream.pipe(parse({ delimiter: ',', columns: true }));

    parser.on('readable', () => {
      let record;
      while ((record = parser.read()) !== null) {
        records.push(record);
      }
    });

    await finished(parser);

    return records;
}

csv-parse + stream/promises

about 4 years ago · Juan Pablo Isaza Report

0

Based on advice given in the answers, I am going to ignore the Airbnb Style Guide and use the Async iterator method.

Final code:

const { parse } = require('csv-parse');
const fs = require('fs');
const path = require('path');
const debug = require('debug')('app:csv:service');
const chalk = require('chalk');

async function parseCsv(csvFile) {
  try {
    const records = [];
    const stream = fs.createReadStream(csvFile);
    const parser = stream.pipe(parse({ delimiter: ',', columns: true }));
    // eslint-disable-next-line no-restricted-syntax
    for await (const record of parser) {
      records.push(record);
    }
    return records;
  } catch (error) {
    debug(`${chalk.red('Failed')} to parse CSV`);
    debug(`${chalk.red('ERROR:')} ${error}`);
    throw error;
  }
}

It may be time to find a new Style Guide to follow. Thank you to num8er for the code advice (I took one of your ideas to make my code a little more readable).

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!