Search code examples
node.jsecmascript-6es6-promise

reading two csv files at a time with promises nodejs


I am new to promises. I want to read two csv files at time using promises. How to read 2 csv files parallel and then proceed to chain. I have gone through this but they have used some library called RSVP. can any one help me how to do this without using any of library functions. I want to read 2 files at a time and should able to access both files in next .then()?

file = require('fs')

// Reading file
let readFile =(filename)=>{
    return new Promise(function(resolve, reject){
        file.readFile(filename, 'utf8', function(err, data){
            if(err){
                reject(err)
            }else{
                resolve(data)
            }
        });
    });
}



// Get the match id for season 2016
getMatchId=(matches)=>{
    let allRows = matches.split(/\r?\n|\r/);
    let match_id = []
    for(let i=1; i<allRows.length-1; i++){
        let x = allRows[i].split(',')
        if(x[1] === '2016'){
            match_id.push(parseInt((x[0])))
        }
    }
    return match_id
}



//  Final calculation to get runs per team in 2016
getExtraRunsin2016=(deliveries, match_id)=>{
    let eachRow = deliveries.split(/\r?\n|\r/);
    result = {}
    let total = 0
    for(let i=1; i<eachRow.length-1;i++){
        let x = eachRow[i].split(',')
        let team_name = x[3]
        let runs = parseInt(x[16])
        let id = parseInt(x[0])
        if(match_id.indexOf(id)){
            total+=runs
            result[team_name] += runs
        }else{
            result[team_name] = 0
        }
    }
    console.log(result, total)
}



// Promise
readFile('fileOne.csv')
    .then((matchesFile)=>{
        return getMatchId(matchesFile)
    })
    .then((data)=>{
        readFile('fileTwo.csv')
        .then((deliveries)=>{
            getExtraRunsin2016(deliveries, data)
        })
    })
    .catch(err=>{
        console.log(err)
    })


Solution

  • You can Promise.all() to combine things without using any other libraries

    "use strict";
    
    Promise.all([
      readFile('fileOne.csv'),
      readFile('fileTwo.csv'),
    ]).then((results) => {
      let [rawFileOneValues, rawFileTwoValues] = results;
    
      // Your other function to process them
    }).catch(err => {
      console.log(err)
    });