0
<script>

$(document).ready(function(){
    $.get("https://ipinfo.io", function(response){
      var city = response.city;
      $("#details").html(JSON.stringify(response, null, 4));
      }, "jsonp");
});

alert(city);

</script>

Is there any possible way to convert $.get asynchronous variable to a classic one that can be printed inside div or span?

I know that usually returns promise, is there no possible way in JS to convert promise to some variable and make it global so all other functions can access it?

laniakea
  • 81
  • 1
  • 11
  • It's not defined when you alert it. It's defined after the ajax call gets back. – Jeremy Thille Sep 18 '20 at 07:04
  • So yes: To continue, call a function where you now have `var city = response.city;` – mplungjan Sep 18 '20 at 07:06
  • In the API documentation of the URL you're calling, it looks like you are required to also post a token, and a more specific route. You seem to be calling the home page. – killstreet Sep 18 '20 at 07:35

0 Answers0