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

137
Views
Wait for a async function to finish before moving on?

I am currently having problem await for previous async function being complete before moving onto next function as the next relies on a value from the other. In the example below getData can not be ran before type is defined

app.get("api/v1/demo",async(req,res)=>{
  try{
      const id = req.query.id
      let type = req.query.type
      if(id){
          if(!type || type==="undefined)){
              type = await findType(id)
          }
          if(type && id){   
          const value = await getData(type,id)
          }
          res.status(200).json({
              status: "sucsess",
              geoJson: data.geoJson,
              sorting: data.sorting,
              options: values.sorting
            })
          //MY problem is that getData relies on type not being undefined but how can I make sure to wait for that?

      }catch(err){
        console.log(err)
      }
})

async function findType(id){
    axios.get(
          "https://data.ssb.no/api/v0/dataset/list.json?lang=no"
        ).then((res) => {
          const datasetValue = res.data.datasets.filter((dataset) => dataset.id === id)
          if (datasetValue[0]["tags"].includes("kommuner")) {
            regionType = "kommune"
          }
          else if (datasetValue[0]["tags"].includes("fylker")) {
            regionType = "fylke"
          }
        else{
            return "Failed"
        }
        })
}

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!