Here I am trying to call every row in a csv file and inside that ".on" we can access every row so I am fetching some data there and then pushing that fetched data into the an array but I see that it call the fetch and then directly goes to next row instead of pushing the data into the array. Any idea on why it is not pushing the data into the array and then go to the next iteration?
var dataArray = [];
fs.createReadStream('./data.csv')
.pipe(csv())
.on('data', async function (row) {
let data = await fetch(row.data);
console.log(data);
row.flag = data;
dataArray.push(row);
})
.on('end', async function () {
console.log('data', dataArray);
var result = json2csv(dataArray);
fs.writeFileSync('./file.csv', result);
});
Here is how I did it, First pushed all the row data form on("data" )inside createreadstream into an array "arr". Then as @ChrisG suggested, Created a array called promise and pushed all the promises into the array. And at the end called Promiss.all(promises[]).then I got what I wanted. @Bravo
let arr = [];
let promises = []
fs.createReadStream(path)
.pipe(csv())
.on('data', async function (row) {
arr.push(row)
})
.on('end', function () {
console.log("data",arr.length)
arr.forEach(async e => {
let msg
dataArray.push(e)
promises.push(fetch(msg))
}
})
Promise.all(promises).then((data) => {
console.log("data",dataArray)
data.forEach((e, index) => {
let data = e.data
dataArray[index].data = data
})
var result = json2csv(dataArray);
fs.writeFileSync(path, result)
})
})
}