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

140
Visualizações
ExpressJS fetching json without then

How do you download a json file within an ExpressJS app using node-fetch

function getData(url) {
    fetchSettings = {method: "Get"};

    fetch(url, fetchSettings)
        .then(res => res.json())
        .then((json) => {
            console.log(json); // <---- This shows the correct json object in the terminal
            return json;
        })
        .catch(function (err) {
            console.log(err);
        });
}

app.get("/", (req, res) => {
   url = "API JSON URL"
   json = getData(url);
   // Need to make multiple getData to different URLs
   console.log(json); // <--- Returns "undefined"
});

I've also tried with await, but I believe I am misusing it

async function getData(url){
    return await fetch(url, fetchSettings)
    .then(res => res.json())
}

app.get("/", (req, res) => {
   url = "API JSON URL"
   json = getData(url);
   // Need to make multiple getData to different URLs
   console.log(json); // <--- Returns "Promise { <pending> }"
});

I need to make multiple getData() calls within the app.get() function in order to build an output json to serve to the webpage. So writing the rest of the code within the then() method within the fetch function is not an option.

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