-1

I need to return lat-lng coordinates from google geocode api, and do something with it. So, my code seems like this:

async function getJson(url){

     try {
           const response = await fetch(url);
           return await response.json();
     }
     catch (err) {
           console.error(err.message);
     }
}
function getLocationForName(address){

      getJson(geoCodeUrl+address+apiKey).then(function(location){

            return location.results[0].geometry.location;
      });
}

someFunc(){
    f['location'] = getLocationForName(city+f['street']+'+'+f['house']);
}

But f['location'] alwais have undefined value

Nisu
  • 185
  • 1
  • 1
  • 12
  • 2
    You forgot to `return getJSON(...).then(...);` from `getLocationForName`. It will return a promise, i.e. `f['location']` will be assigned the promise. If that's not what you want then wait for the promise to resolve first: `getLocationForName(...).then(location => f['location'] = location)`. Maybe have a look at [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/218196) if you are not 100% sure how async functions work. – Felix Kling Aug 23 '17 at 19:09
  • Yep, this example really helpfull, thanks! So, `getLocationForName(...).then(location => f['location'] = location)` its look very gracefully, but I get this error: _Uncaught TypeError: Cannot read property 'then' of undefined_ – Nisu Aug 23 '17 at 19:42
  • Returning await also returns the promise, not the result. You have to assign the result to a variable _then_ return the value of that variable. –  Aug 23 '17 at 19:58
  • I'm very slowly tonight =( Can you print an example? – Nisu Aug 23 '17 at 20:11
  • *"but I get this error:..."* You still have to do `return getJSON(...).then(...);` in `getLocationForName`. – Felix Kling Aug 24 '17 at 01:23

1 Answers1

0

You need to use a callback function:

function getLocationForName(address, callback){
      getJson(geoCodeUrl+address+apiKey).then(function(location){
        if (callback) callback(location.results[0].geometry.location); 
      });
}
someFunc(){
    getLocationForName(city+f['street']+'+'+f['house'], function(location){
       f['location'] = location;
    });
}
Nix
  • 176
  • 1
  • 10
  • Yep, this method work fine, if use `f['location']` in js, but I need to put all `f` array to php. So, in the `$_POST['insertDailyQuest']` this array does not exist. [image](http://i.piccy.info/i9/a6957b5a2d27343d13e859aa18778478/1503518601/29028/1173793/js_and_php_response.png) `getLocationForName(city+f['street']+'+'+f['house'], function(location){ f['location'] = location; }); $.post('php/proxy.php',{insertDailyQuest: f}, function(response){ console.log(f); console.log(response); });` – Nisu Aug 23 '17 at 20:05
  • All the processing that depend on the location should go inside the callback function: `getLocationForName(city+f['street']+'+'+f['house'], function(location){ f['location'] = location; $.post('php/proxy.php',{insertDailyQuest: f}, function(response){ console.log(f); console.log(response); }); }); ` – Nix Aug 23 '17 at 20:24