Abstracting callbacks
In this section, we'll be refactoring app.js
, taking a lot of the complex logic related to geocoding and moving it into a separate file. Currently, all of the logic for making the request and determining whether or not the request succeeded, our if else
statements, live inside of app.js
:
request({ url: `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}`, json: true }, (error, response, body) => { if (error) { console.log('Unable to connect Google servers.'); } else if (body.status === 'ZERO_RESULTS') { console.log('Unable to find that address.'); } else if (body.status === 'OK') { console.log(`Address: ${body.results[0].formatted_address}`); console.log(`Latitude: ${body.results[0].geometry.location.lat}`); console.log(`Longitude: ${body.results[0].geometry.location.lng}`); } });
This is not exactly reusable and it really doesn't belong here. What I'd like to do before we add even more logic related to fetching...