# Return value of inner function from outer function?

**URL:** <https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125>\
**Category:** I Built This\
**Created:** [November 2, 2016, 2:24pm UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125 "2016-11-02T14:24:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inhert](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/inhert/32/18004_2.png) [@Inhert](https://forum.freecodecamp.org/u/Inhert)\
**Post date:** [November 2, 2016, 2:24pm UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125/1 "2016-11-02T14:24:15Z")

</div>

Hi guys, this is the first time I’m REALLY stucked. Thing is I have this code:

```
function getLon(){ 
   var lon; 
   $.getJSON("http://ip-api.com/json", function(pos){
      lon = (pos.lon).toString(); 
   }); 
return lon; };

```

And (of course) lon isn’t in the scope of the outer function, so it doesn’t return anything. My question is, how could I return the lon value using the $.getJSON?

Thanks.

---

<div class="post-metadata">

**Author:** ![zsoltime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zsoltime/32/4252_2.png) [@zsoltime](https://forum.freecodecamp.org/u/zsoltime)\
**Post date:** [November 2, 2016, 3:00pm UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125/2 "2016-11-02T15:00:38Z")

</div>

The problem is that `$.getJSON` is asynchronous and you want to use it like it were synchronous. I’d suggest you to read up on the topic, it’s something **you will definitely need later**.

Anyway, you can work with the `lon` variable inside your callback or you can use [promises](https://davidwalsh.name/write-javascript-promises). I hope it helps and can’t wait to see your weather app. 😉

---

<div class="post-metadata">

**Author:** ![Inhert](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/inhert/32/18004_2.png) [@Inhert](https://forum.freecodecamp.org/u/Inhert)\
**Post date:** [November 2, 2016, 3:32pm UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125/3 "2016-11-02T15:32:59Z")

</div>

Thanks zsoltime, I’ve read it but my code doesn’t work, I think I didn’t understand the way it works…:

```
var lon = {
  html: function() {
    return $.getJSON('http://ip-api.com/json').then(function(data) {
      return data.lat;
    });
  }
};

lon.html().done(function(html) {
  $("body").append(html);
});

```

This should do something, right? Even if I copy/paste de example code, it doesn’t work 😕

---

<div class="post-metadata">

**Author:** ![JohnL3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnl3/32/16459_2.png) [@JohnL3](https://forum.freecodecamp.org/u/JohnL3)\
**Post date:** [November 2, 2016, 3:53pm UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125/4 "2016-11-02T15:53:09Z")

</div>

watch this it should help … [https://channel9.msdn.com/Series/Javascript-Fundamentals-Development-for-Absolute-Beginners/Using-jQuery-to-Retrieve-JSON-via-AJAX-19](https://channel9.msdn.com/Series/Javascript-Fundamentals-Development-for-Absolute-Beginners/Using-jQuery-to-Retrieve-JSON-via-AJAX-19)

---

<div class="post-metadata">

**Author:** ![zsoltime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zsoltime/32/4252_2.png) [@zsoltime](https://forum.freecodecamp.org/u/zsoltime)\
**Post date:** [November 2, 2016, 4:36pm UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125/5 "2016-11-02T16:36:05Z")

</div>

It does actually, it appends the latitude of my location to the body. What error do you get, if any?

---

<div class="post-metadata">

**Author:** ![Inhert](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/inhert/32/18004_2.png) [@Inhert](https://forum.freecodecamp.org/u/Inhert)\
**Post date:** [November 2, 2016, 7:18pm UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125/6 "2016-11-02T19:18:00Z")

</div>

No errors shown. It just doesn’t show anything.

[Here’s my attempt](https://codepen.io/Inhert/pen/MjMrpb). I just want two variables (lat & lon) to acquire the latitude and longitude given by [this request](http://ip-api.com/json) and use these variables at the openweathermap request.

The only thing I could do from my point of view is to store the data in a hidden div and get it back in the request, but I know it must be a better way to do it. A correct way to do it. And I’m stucked already two whole days, it’s frustating.

---

<div class="post-metadata">

**Author:** ![Inhert](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/inhert/32/18004_2.png) [@Inhert](https://forum.freecodecamp.org/u/Inhert)\
**Post date:** [November 3, 2016, 12:52am UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125/7 "2016-11-03T00:52:14Z")

</div>

Ok, I managed to get some data. The answer was simplier than I could imagine… the problem now is that I’m doing a request by IP and the location isn’t exact.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:04pm UTC](https://forum.freecodecamp.org/t/return-value-of-inner-function-from-outer-function/51125/8 "2021-06-01T18:04:05Z")

</div>

Closed on June 1, 2021, 6:04pm UTC


