In my geolocation app, I have been effectively utilizing promises to make asynchronous AJAX requests that return API JSON objects. Using the then() function, I have successfully chained and processed each promise's data accordingly.
However, I have encountered an issue with the last Promise in my sequence. This particular Promise does not involve calling an API; instead, it involves fetching geodata from a file. While I can easily return this data, my challenge lies in returning both the data and an additional variable from the final step of the chain. Below is the snippet of code pertaining to this problem:
// ---- Continuing from the previous promise chain ---- //
return openWeather
}).then(function(data4) {
console.log(data4)
// The following code snippet needs to be addressed
$('#txtDescription').html(data4['data'][0]['weather']['description']);
$('#txtSpeed').html(data4['data'][0]['wind_spd'] + ' mph');
$('#txtDirection').html(data4['data'][0]['wind_dir'] + ' °');
$('#txtTemp').html(data4['data'][0]['temp'] + ' ° celcius');
// Data to be returned
let iso2 = data4['data'][0]['country_code'];
// Function to be returned
return geoData(iso2);
}).then(function(result) {
// Attempting to assign two values from the returned array
let iso2 = result[0]
let data5 = result[1]
// Both console.logs yield undefined.
console.log(iso2)
console.log(data5);
All my AJAX calls are stored in a separate file and imported as modules for use.
function geoData(iso2) {
return $.ajax({
type: "GET",
url: "countries.geo.json",
dataType: 'json',
success: function (data5) {
// Both variables are logged to the console within this success function
console.log(iso2)
console.log(data5)
// Returning both values by using an array
return [iso2, data5]
}
});
};
While I can individually return either the data from the 'geoData()' AJAX call or the 'iso2' variable, I am unable to retrieve both simultaneously. Essentially, I require the variable for comparison in a conditional/loop that matches it with a specific country listed in the returned JSON object.
Is there a way to return two values rather than just one? Any guidance on resolving this dilemma would be greatly appreciated.