Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
javascript forcing multiple http get requests to be synchronous using async/await

I have a module exporting a function who's goal is to collect all streamed string data from a URL and return the complete result:

'use strict'
const http = require("http");
const bl = require("bl");

async function getUrlData (url) {
  http.get(url, (response) => {
    response
      .pipe(bl((err, data) => {
        if(err) return console.log(err);
        
        const stringData = data.toString();
        return stringData
      }));
  });
}

module.exports = getUrlData;

I also have a main file which accepts 3 URLs as arguments and attempts to print out the value from getting the data from each URL in the exact order given.

'use strict'
const getData = require('./http-collect-module');

const urls = process.argv.slice(2);

const main = async () => {
  urls.forEach((url) => {
    var stringData = await getData(url);
    console.log(stringData)
  });
}

main();

I'm trying to make one get complete before moving onto the next, however am receiving this error:

    var stringData = await getData(url);
                     ^^^^^

SyntaxError: await is only valid in async function

I cannot understand why I'm getting this error since the main function is an async one. Any help appreciated

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!