I was trying to parse csv file and according to the information fetched, I am making an api call with it.
Here is the code:
let booklist = [];
let updatedBook;
fs.createReadStream("Book1.csv")
.pipe(csv())
.on('data', async function(data){
console.log("data");
updatedBook = await apiCall(data.title, data.author, booklist);
})
console.log(updatedBook);
return updatedBook;
The expected behavior here is print data
, execute apiCall()
function, get the next data in CSV
file, print data
, execute apiCall()
and so on.
however, it does not await
for the apiCall
to be finished and reads the next data in the CSV file. So, at last, it just returns undefined updatedBook
.
It returns proper updatedBook
if I sleep for 10 seconds and wait for all the apiCall to be finished but I don't think this is the right way to do.
What am I missing here? is await not supposed to work in this particular function?
Your answer will be very much appreciated! Thanks!!