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