0

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)
    })
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
Pramod K
  • 3
  • 4

2 Answers2

0

You want to use Promise.all().

// Promise
Promise.all([
  readFile('fileOne.csv').then(getMatchId),
  readFile('fileTwo.csv'),
])
  .then(([data, deliveries]) => getExtraRunsin2016(deliveries, data))
  .catch(err => console.log(err));

I also recommend using fs-extra, which would replace your readFile implementation.

murrayju
  • 1,752
  • 17
  • 21
0

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)
});
Sridhar
  • 11,466
  • 5
  • 39
  • 43