I'm using the Yelp API to get a list of businesses in a certain area. I call my function 'searchForBusinesses()' which makes a request to my backend server code and gets a response in the form of a list of 10 businesses.
function searchForBusinesses(parameters){
var businesses = [];
$.get('yelp', parameters, function(res){
for(var i = 0; i < 10; i++){
var business = {
title: res.businesses[i].name,
location: { lat: res.businesses[i].coordinates.latitude, lng: res.businesses[i].coordinates.longitude },
id: res.businesses[i].id
};
businesses.push(business);
}
});
return businesses;
}
When I call my function
var businesses = searchForBusinesses(parameters);
console.log(businesses);
console.log(businesses.length);
I get
For some reason, I can log the list with all of the businesses in there correctly, but when I log the length of that list, I get 0. This is highly unusual.