title: How to make a synchronous http get in nodejs?
body: <p>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...</p>
  <pre><code>const fs = require(&quot;fs&quot;);
  const https = require(&quot;https&quot;);
  var text = &quot;&quot;
  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&lt;issns.length; i++){
          url = &quot;some_api_url&quot; + issns[i]
          https.get(url, (resp) =&gt; {
              let data = '';
              resp.on('data', (chunk) =&gt; {
                  data += chunk;
              });
              resp.on('end', () =&gt; {
                  data = JSON.parse(data)
                  constructCSV(issns[i], data)
              });
              }).on(&quot;error&quot;, (err) =&gt; {
                  console.log(&quot;Error: &quot; + err.message);
          });
      }
  }
  function constructCSV (issn, data){
      // code to get some data from the json in parameter &quot;data&quot;
      // ...
      // ...
      saveOutput()            
  }
  function saveOutput(){
      fs.writeFileSync(output_file_dir, text);
  }
  </code></pre>
  <p>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.</p>
  <p>If anyone could help I would be very thankful!</p>