title: Mozilla WebExtension API storage - Debugging with and without breakpoints leads to different output
body: <p><br/>
  Hello guys,
  <br/>
  i am trying to implement an add-on for the Mozilla-Firefox Browser.
  The following script shows one backgorund script I already successfully integrated. It uses the Mozilla WebExtension API storage. It gets executed but the log on the browser console suprises me.
  I get alternating logged nothing and: <br/></p>
  
  <pre><code>bla
  bla
  bla
  </code></pre>
  
  <p>If and only if I set breakpoints on significant lines (especially the last 5 lines) of my code in Debugging-Mode I get always the expected result:</p>
  
  <pre><code>bla
  </code></pre>
  
  <p>How is it that the output depends on setting breakpoints.
  I have no idea what is happening and can not find any similar problems on the internet. Can somebody explain to me what is happening and what I can do to prevent the wrong output?<br/><br/>
  storeManager.js:<br/></p>
  
  <pre><code>var localStore = chrome.storage.local;  
  var StoreManager = function(){};
  
  StoreManager.prototype.addItem = function(type, item){
      var thisRef = this;
  
      localStore.get(type, 
          function(obj){
              if(chrome.runtime.lasterror){
                  console.log(chrome.runtime.lastError);
              }else{
                  var oldArr = obj[type];
  
                  if(oldArr === undefined)
                      oldArr = [];
  
                  if(oldArr.indexOf(item) !== -1)
                      return;
  
                  oldArr.push(item);
                  thisRef.setItem(type, oldArr);
              }
          }
      );
  }
  
  StoreManager.prototype.removeItem = function(type, item){
      var thisRef = this;
  
      localStore.get(type, 
          function(obj){
              if(chrome.runtime.lasterror){
                  console.log(chrome.runtime.lastError);
              }else{
                  var oldArr = obj[type];
  
                  if(oldArr === undefined)
                      return;
  
                  var index = oldArr.indexOf(item);
                  if(index === -1)
                      return;
  
                  oldArr.splice(index, 1);
                  thisRef.setItem(type, oldArr);
              }   
          }
      );
  }
  
  StoreManager.prototype.setItem = function(type, item){
      localStore.set({ [type] : item }, 
          function(){
              if(chrome.runtime.lasterror){
                  console.log(chrome.runtime.lastError);
              }
          }
      );
  }
  
  StoreManager.prototype.visitItems = function(type, visit){
      localStore.get(type, 
          function(obj){
              if(chrome.runtime.lasterror){
                  console.log(chrome.runtime.lastError);
              }else{
                  var oldArr = obj[type];
  
                  if(oldArr !== undefined){
                      for(var i=0; i&lt;oldArr.length; i++){
                          visit(oldArr[i]);
                      }
                  }
              }
          }
      );
  }
  
  function justLog(str){
      console.log(str);
  }
  
  var sm = new StoreManager();
  sm.visitItems("lol", justLog);
  sm.addItem("lol", "bla");
  sm.visitItems("lol", justLog);
  sm.removeItem("lol", "bla");
  sm.visitItems("lol", justLog);
  </code></pre>
  
  <p><br/>
  manifest.json:<br/></p>
  
  <pre><code>{
      "manifest_version": 2,
      "name": "Addon",
      "version": "0.0",
  
      "description": "no desc",
  
      "background": {
          "scripts": [
              "storeManager.js"
          ]
      },
  
      "permissions": [
          "storage"
      ]
  }
  </code></pre>