title: Chrome extension sendMessage and asynchronous callback
body: <p>I built a Chrome extension using <code>sendRequest</code> to send a message from the content page to the popup, and get a callback.</p>
  
  <p>content.js :</p>
  
  <pre><code>chrome.extension.sendRequest(data, function (result){
      // here the result should be a boolean
      if (result){
          alert("ok");
      }
  });
  </code></pre>
  
  <p>popup.js :</p>
  
  <pre><code>chrome.extension.onRequest.addListener(function (request, sender, callback) {
      // Here I have some asynchronous work
      setTimeout(function(){
          callback(true);
      }, 500);
  }
  </code></pre>
  
  <p>All this worked perfectly fine.</p>
  
  <p>But the <code>sendRequest</code> is deprecated, and needs to be replaced by <code>sendMessage</code> (in order to support Firefox)</p>
  
  <p>I did almost the same thing with <code>sendMessage</code> (documentations are exactly the same for <a href="https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/Tabs/sendRequest" rel="nofollow"><code>sendRequest</code></a> and <a href="https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/Tabs/sendMessage" rel="nofollow"><code>sendMessage</code></a> on Mozilla doc)</p>
  
  <p>content.js :</p>
  
  <pre><code>chrome.runtime.sendMessage(data, function (result) {
      // here the result should be a boolean
      if (result){
          alert("ok");
      }
  });
  </code></pre>
  
  <p>popup.js :</p>
  
  <pre><code>chrome.runtime.onMessage.addListener(function (message, sender, callback) {
      // Here I have some asynchronous work
      setTimeout(function(){
          callback(true);
      }, 500);
  }
  </code></pre>
  
  <p>But now, it doesn't work. As soon as the listener function ends, the callback is called with no argument, and the asynchronous function can't send its result.</p>
  
  <p>Is there any way to do the same thing in Firefox ?</p>