title: JavaScript: Use variable outside of fetch function
body: <p>I would like to use the returned IP-adress in my normal JavsScript code outside of fetch function. Everything works fine when I print out the IP-adress inside the fetch funtion. But as soon as I try to log &quot;ip&quot; outside of it, it returns &quot;undefined&quot;. I know this may be because fetch is asynchronus, but I do not know how to solve this problem. I would really appreciate some help!</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code> 
  function getIP(){
    let ip;
    fetch('https://api.ipify.org/?format=json')
      .then(result =&gt; result.json())
      .then(data =&gt; {
        ip = data.ip;
        console.log(ip);
      });
    console.log(ip);
  }
        </code></pre>
  </div>
  </div>
  </p>