title: Consecutive calls to chrome.extension.sendMessage()
body: <p>I am writing an extension, where I want my own cookies store <code>mycookies</code> in <code>background.js</code> which stores domain, path, key and flags. In my <code>background.js</code>, when an HTTP response is received, I call <code>chrome.extension.sendMessage()</code> for each Set-Cookie header of the the response. So each response call this function many times within few m-seconds. After the response, only the last one or two cookies are stored, the rest are lost.</p>
  
  <p>I did the following experiment. I connected a button with the function <code>update_mycookies</code>. A click on button calls the function immediately and displays the content of <code>mycookies</code> after 2500ms. If I read the store after 2500ms, all four cookies are there if I introduce the delays (as below) in <code>update_mycookies</code> but if I remove the delays, only the last one or two cookies are updated, the rest are lost. The <code>chrome.extension.sendMessage</code> is using `chrome.storage.local.set(). I want my store must be updated with all the cookies in response.</p>
  
  <pre><code>function update_mycookies(){
      chrome.extension.sendMessage({"cdomain":".google.com", "path":"/", "key":"PREF"});
      setTimeout(function() {
          chrome.extension.sendMessage({"cdomain":".google.com", "path":"/", "key":"NID"});}, 1000);
  
      setTimeout(function(){
          chrome.extension.sendMessage({"cdomain":".google.it", "path":"/", "key":"PREF"}); }, 1500);
  
      setTimeout(function(){
          chrome.extension.sendMessage({"cdomain":".google.it", "path":"/", "key":"NID"}); }, 2000);
  }
  </code></pre>
  
  <p>The <code>onMessage</code> listenr is the following: </p>
  
  <pre><code>chrome.extension.onMessage.addListener(  
    function(request,sender,sendResponse) {
   if (request["cdomain"] &amp;&amp; request["path"] &amp;&amp; request["key"]) {
         chrome.storage.local.get("mycookies", function (items) {
           if (items["mycookies"]) {
            var found = false;
             var entries = items["mycookies"].split(";");
             entries.forEach (function (entry) {
              var dk = entry.split(",");
               if (dk[0] == request["cdomain"] &amp;&amp; dk[1] == request["path"] &amp;&amp; dk[2] == request["key"])
                 found = true; 
             });
             if (!found)
               chrome.storage.local.set({"mycookies": items["mycookies"] + ";" + request["cdomain"] + "," + request["path"] + "," 
                 + request["key"] });
           }
          else 
       chrome.storage.local.set({"mycookies": request["cdomain"] + "," + request["path"] + "," + request["key"] });
        }); 
     }
   }
  </code></pre>