0

I have function which take opened tabs and save it into chrome.storage, problem is that I need to execute function chrome.windows.getAll() first, but if my function runs it skips function chrome.windows.getAll() and after whole function it will jump back to function chrome.windows.getAll() and execute it as last.

This is my code:

    var new_tabs = [];

    chrome.windows.getAll({populate:true},function(windows){
      windows.forEach(function(window){
        window.tabs.forEach(function(tab){
          new_tabs.push(tab.url);
        });
      });
    });

    chrome.storage.local.set({'tabs': new_tabs});
tomsk
  • 967
  • 2
  • 13
  • 29
  • Since getAll is asynchronous, save the data inside its callback, right after windows.forEach. You may want to see more examples/tutorials of how asynchronous code works. – wOxxOm Jun 25 '18 at 09:08
  • Oh I didn't know that it is asynchronous, I would like to save data to some variable which is "visible" to whole function and not only inside its callback. – tomsk Jun 25 '18 at 09:27
  • There's no point in making it visible because the actual value is assigned only inside the asynchronous callback, which is invoked later, after the enclosing function is completed. See the linked topic for more details. – wOxxOm Jun 25 '18 at 09:32

0 Answers0