I am reading a CSV using CSV-parser npm module and have to perform some operation on the data I get from the CSV (for each line).
const readstream = fs.createReadStream('src/working_file.csv');
const stream = readstream.pipe(parser());
stream.on('data', async data => {
// data is a JSON object of the row in CSV.
// Now i am calling another async function from user using the data in the JSON
console.log('before calling');
const writeToFile = await getImage(data.searchKey);
console.log('after calling');
// do other stuff
}
async function getImage(searchKey){
// im doing web scraping here using puppeeter
// it has some await calls too
console.log('in getimage');
const results = await scrapper.run().catch(err => {
console.error(err);
process.exit(1);
});
}
let say my csv has 2 rows then, my output is coming like below
before calling
in getimage
before calling
in getimage
after calling
after calling
but when I am doing this all callings are happening at a time though I used await. If I have 10 rows in the CSV all 10 rows calling the function is happening at the same time. but I want it to happen one by one. Only when the operation with the first row completes then I want the operate the second row.
my problem is all calls are happening at once rather than once by one.