title: Accessing object data inside async function in the global scope
body: <p>I need to access the token so I can utilize it in other places in my application. There is a scope issue I am running into, and I have set a variable outside of the function (I have seen that in other use cases it works, if you return to it.)</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-html lang-html prettyprint-override"><code>let myToken;
  const getToken = async function () {
    try {
      const fetchToken = await fetch(
        'https://api.website.com/v1/authenticate',
        {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            email: 'username123',
            password: 'password123',
          }),
        }
      );
      const tokenData = await fetchToken.json();
      return (myToken = tokenData.data.token);
    } catch (err) {
      console.error(err);
    }
  };
  
  getToken();
  console.log(myToken);</code></pre>
  </div>
  </div>
  </p>
  <p>Undefined is returned.</p>
  <p>Thanks for taking a look.</p>