I have a code that calls an API based on values inside a txt file. Out of each API call, the code extracts some information from the response JSON and constructs a csv. I have gotten my code to work by overwriting the csv each time I finish an HTTP get call to the API. However, I would prefer to only write the csv once for efficiency's sake. My current code is something like this...
const fs = require("fs");
const https = require("https");
var text = ""
fs.readFile(filename, 'utf8', function(err, data) {
if (err) reject(error);
issns = data.split(/\r?\n/)
getPolicies(issns);
});
function getPolicies(issns){
for (let i = 0; i<issns.length; i++){
url = "some_api_url" + issns[i]
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
data = JSON.parse(data)
constructCSV(issns[i], data)
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
}
function constructCSV (issn, data){
// code to get some data from the json in parameter "data"
// ...
// ...
saveOutput()
}
function saveOutput(){
fs.writeFileSync(output_file_dir, text);
}
As mentioned I would prefer to only call saveOutput() once, if possible right after calling getPolicies(). However that results in a blank csv file since get policies uses the asynchronous http get.
If anyone could help I would be very thankful!