Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda