0

I am using a library called node-geocoder in express js which has the following code:

var NodeGeocoder = require('node-geocoder');

var options = {
  provider: 'google',

  // Optional depending on the providers
  httpAdapter: 'https', // Default
  apiKey: 'YOUR_API_KEY', // for Mapquest, OpenCage, Google Premier
  formatter: null         // 'gpx', 'string', ...
};

var geocoder = NodeGeocoder(options);

// Using callback
geocoder.geocode('29 champs elysée paris', function(err, res) {
  console.log(res);
});

The response variable(res) in the geocode method's callback function holds an object with location properties such as latitutde and longitude. The link for this package is here

I was wondering if there was a way to use that response variable outside of the callback function in the geocode method. I need to pull the latitude and longitude properties and I don't want to keep the rest of the code within that callback function.

As a noob I tried just returning the object and storing it in a variable like so:

var object = geocoder.geocode('29 champs elysée paris', function(err, res) {

   return res;

});

This doesn't work since it's in the callback and not returned in the actual geocode method.

1
  • One way to solve it is to call a function from the callback. See my answer below Commented Apr 2, 2018 at 17:39

4 Answers 4

1

Not directly, but there are a couple of options to get closer to that.

One would be to have your callback be a defined function and use that as the callback:

const doSomething = (err, res) => { /* do something */ }

geocoder.geocode('abc', doSomething);

Not really much different, but can make it a little cleaner.

You can also "promisify" the function to have it return a Promise. Something like this would do the trick:

const geocodePromise = (path) => new Promise((resolve, reject) => {
    geocoder.geocode(path, (err, res) => err ? reject(err) : resolve(res));
});

geocodePromise('abc')
  .then(res => { /* do something */ })
  .catch(err => { /* do something */ });

Finally, if you are using Babel for transpiling (or Node version 7.6.0 or higher, which has it natively), you can use async and await. Using the same promisified version of the function as above, you'd have to wrap your main code in an async function. I generally use a self-calling anonymous function for that:

(async () => {
  try {
    const res = await geocodePromise(path);

    /* do something with res */
  } catch (err) {
    /* do something with err */
  }
})();

With this, you get the closest to what you want, but you'll still have to wrap your main code up in a function because you can't await at the top level.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't want to keep the rest of the code within that callback function.

The code doesn't have to be in the callback, it just has to be called from the callback.

So you write your function (or functions) as normal:

function handleInfo(info) {
    doSomethingWithInfo(info);
    doSomethignElse(info);
    // ...
}
// ...

...and then call those functions when you have the data:

geocoder.geocode('29 champs elysée paris', function(err, info) {
  if (err) {
      // Handle error
  } else {
      handleInfo(info);
  }
});

Comments

0

You can use the response of your function outside the callback by calling another function.

geocoder.geocode('29 champs elysée paris', function(err, res) {
  if(!err){
    // call a function if there is no error
    console.log(res);
    myFunction();
  }
 });

function myFunction(){
 //do your processing here
}

Comments

0

geocode can return promise already, no need to re-wrap it. If you don't care about the error and just want to grab the response's location data. You can do this

var locationData = geocoder.geocode('29 champs elysée paris').then( function(res){
    return res;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.