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

74
Visualizações
How to continue the function after fetching

I have a script currently that needed to fetch data:

fetch("https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=flw&lang=en")
.then(r => r.json())
.then(result => console.log(result))

If I had to continue the script, I can use these methods:

Method 1:

function stuff(){
   ...
   .then(result => function(){ <code> })
}

Method 2:

function stuff(){
   ...
   .then(result => fetchcont(result))
}
function fetchcont(r){
   <code>
}

Is there a way that this can be presented in a neater way, for example:

function stuff(){
   ...
   .then(result => *continue*)
   <script goes here>
}

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would suggest you to use async/await instead of .then().

Your code would look something like:

// your function need to be async in order to use await keyword
async function stuff(){
try {
//await
const res = await fetch(url);
// next line will only execute when above fetch is complete
const json = res.json()

*Do Your other stuff here*

} catch (e) {
console.log("Err",e)
  }
}

You can read more about async/await here.

about 4 years ago · Juan Pablo Isaza Relatório
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