title: How to make chrome.webRequest wait for results of XMLHttpRequest();
body: <p>I am working on a Chrome Extension where I want to block the loading of a page based on an external lookup (e.g., check a spam rating or other service based on the URL the user typed into the entry bar).</p>
  
  <p>From the Chrome Developer Docs, they provide the <a href="https://developer.chrome.com/extensions/webRequest" rel="nofollow noreferrer">following basic example for blocking</a>:</p>
  
  <pre><code>chrome.webRequest.onBeforeRequest.addListener(
      function(details) { return {cancel: true}; },
      {urls: ["*://www.evil.com/*"]},
      ["blocking"]);
  </code></pre>
  
  <p>The problem I am having is trying to figure out the right way of using callbacks, promises, or async/await so I can determine if I want to return true and block the web page from loading.</p>
  
  <hr>
  
  <p>For example, this will work:</p>
  
  <pre><code>function returnTrue(){
    return {cancel: true};
  }
  
  chrome.webRequest.onBeforeRequest.addListener(
    returnTrue,
    {
      urls: ["&lt;all_urls&gt;"],
      types: ["main_frame"]
    },
    ["blocking"]);
  </code></pre>
  
  <p>but if I make <code>returnTrue()</code> async the listener no longer blocks, and the page loads. e.g.,</p>
  
  <pre><code>async function returnTrue(){
    return {cancel: true};
  }
  </code></pre>
  
  <hr>
  
  <p>In my real function I tried using await on the results of a promise which wraps 
   <code>XMLHttpRequest()</code>. Prior to that, I tried making it synchronous when I call <code>XMLHttpRequest().open()</code> by setting the last parameter false. This cause execution to by synchronous, but its deprecated functionality and it cause some other weird problems that made ,send() fail with errors. I would therefore like to avoid forcing <code>.open()</code> synchronous.</p>
  
  <p>I also tried putting the <code>async</code> keyword before <code>chrome.webRequest.onBeforeRequest...</code> but that doesn't work either.</p>
  
  <hr>
  
  <p>How can I ensure that I force the Chrome addon to wait until I called the remote service and made a decision before it executes. Since this is blocking the loading, it really should all be synchronous logic or at least await before making the blocking decision.</p>