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
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!
2 Likes
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/
1 Like
ES6 Promise.all() worked great. Thanks guys.
1 Like