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

86
Views
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 answers
Answer question

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