const request = require('postman-request');
const geocode = (address) => {
const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${address}.json?access_token=pk.eyJ1IjoibWVya3VyMTIzIiwiYSI6ImNrYjVndDk3bjBvNGEyeW16cHlid2txZ3YifQ.NGOWOq0yq0wvkhzDzjnUpQ&limit=1`;
request({ url, json: true }, (error, response) => {
const data = response.body;
if (error) {
return 1;
} else if (data.message === 'Not Found' || data.features.length === 0) {
return 1;
} else {
return {
longitude: data.features[0].center[0],
latitude: data.features[0].center[1],
location: data.features[0].place_name,
};
}
});
};
output = geocode("New York");
console.log(typeof output);
Hi guys, I know that the code doesn't work and that I should use a callback function but I was wondering why that's actually the case. Why can't I return the result, which in this case is an object, and access it as normally?
Thanks for every helpful response!