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

101
Views
Combining two JavaScript API requests inside of a function

I am very new to JavaScript and as my current assignment, I am to create a simple weather forecast site using openweathermap.org API. The requirements for my assignment dictate that I must be able to fetch weather data by entering only a city name. However, the openweather API requires that the latitude and longitude be included in the request URL. The Forecast API call URL is: https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key} . For this URL, I think I would write:

let forecastApiUrl = 'http://api.openweathermap.org/data/2.5/onecall?lat=' + lat + '&lon=' + lon + '&exclude=minutely&appid={TopSecretApiKey}';

As luck would have it, openweathermap has a GeoCoding API as well! The API call for GeoCoding is: https://api.openweathermap.org/geo/1.0/direct?q={city name}{state code}{country code}&limit={limit}&appid={API key} . I haven't written the URL variable for this yet.

My question is: How do I go about writing the function such that the contents of the GeoCoding API response will be "read" by the Forecast API call, and output my desired weather forecast? This is only my 2nd API exercise, and the only one I've done requiring this additional step. Any advice much appreciated. Please take into consideration that I am an extreme novice.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This would be better solved with a promise.

You can sync while waiting for your first geocaching API request, and once that's done, it will send the second request to get the weather.

read this

It will look something like this (PSEUDOCODE):

 function fetchGeoData() { return new Promise((resolve, reject) => { //Check if your request was successful, if it was resolve(), else reject() }) } async function fetchWeather() { const geoData = await fetchGeoData(); And now you can just fetch your weather here, the payload would look something like: {lat: geoData.lat, long: getData.long} }

Don't forget to create backups in case any of your requests fail.

about 4 years ago · Santiago Trujillo 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!