Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda