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

139
Views
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
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!