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

141
Vistas
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 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