Basic AJAX question involving OpenWeatherMapAPI

Basic AJAX question involving OpenWeatherMapAPI
0.0 0

#1

Hello,

I want to have both hourly and daily data for my WeatherApp so I need calls from:

api.openweathermap.org/data/2.5/forecast?

and also

api.openweathermap.org/data/2.5/forecast/daily?

Can this be done in one single AJAX call? If so, how?

Thanks


#2

Hi @RadDog25

It’s sort of possible in one call, basically you need to make 2 API calls and wait for both of them to come back.

// jQuery example

var hourly = $.getJSON('hourly_url');
var daily = $.getJSON('daily_url');

$.when(hourly, daily).done(function(hourlyResponse, dailyResponse) {
  // Do something with both.
});

// ES6 Fetch promise example

var hourly = fetch('hourly_url').then(res => res.json());
var daily = fetch('daily_url').then(res => res.json());

Promise.all([hourly, daily]).then(function(resArray) {
  resArray[0] // hourly data
  resArray[1] // daily data
})

For further reading about each method, check out:
https://api.jquery.com/jquery.when/

Hope this helps!


#3

I don’t think you can do them in one call. But you can fire them in parallel with jQuery’s $.when()

http://api.jquery.com/jQuery.when/


#4

ES6 Promise.all() worked great. Thanks guys.