How to make a synchronous http get in nodejs?

Viewed 24

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!

1 Answers

Well, it seems you don't really need synchronous request, but detecting when all the requests are done.

A simple solution would be to track the last request, and then pass it as parameter to constructCSV, which would then call saveOutput upon all requests are done flag:

// send last request flag
constructCSV(issns[i], data, i+1==issns.length)

//...

// write on the last request flag
function constructCSV (issn, data, done){
    // code to get some data from the json in parameter "data"
    // ...
    // ...
    if(done) saveOutput()            
}
Related