title: Error in global call to async function: "await is only valid in async functions and the top level bodies of modules"?
body: <p>Before I begin, I acknowledge that there are several questions on SO which may sound <em>similar</em> to mine going by the title, however, all of them that I read are <em>more</em> complicated than my code and the explanation does not seem to pertain to my situation.</p>
  <p>Can someone please help me understand what is going on in my code (snippet below) that is resulting in this error:</p>
  <blockquote>
  <p>Uncaught SyntaxError: await is only valid in async functions and the
  top level bodies of modules.</p>
  </blockquote>
  <p>As far as I can see, the <code>await</code> which is giving rise to the error <em>is</em> in a &quot;top level&quot; body. Or is something else meant by top level body? Thanks a lot!</p>
  <p><strong>EDIT</strong> for differentiating from the other suggested (similar) question <a href="https://stackoverflow.com/questions/56685338/how-to-resolve-the-syntax-error-await-is-only-valid-in-async-function">here</a>: <em>My question does not deal with httpGet, some other context is different, and most  importantly I have received an answer that solves the problem for me, unlike the suggestion given in the (solitary) answer to the other question. Hence, while I have been able to find a solution here, I believe it will be valuable for the general audience for my question to stay.</em></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>var data;
  await getData();
  document.body.write(data);
  
  async function getData() {
      const res = await fetch("https://jsonplaceholder.typicode.com/posts", {
          method: 'GET',
          headers: {
            'Accept': 'application/json, text/plain, */*',
            'Content-type': 'application/json'
          }
      });
      data = await res.json();
  }</code></pre>
  </div>
  </div>
  </p>