0
tabID = [];

chrome.tabs.query({
    currentWindow: true
}, function(tabs) {
    for (var i = 0; i < tabs.length; i++) {
        tabID.push(tabs[i]["id"]);
    }
});

console.log(tabID[0]);

I'm trying to store the tab IDs in the current window into an array and then do other things with the IDs. However, every time I try to access the array, it says that it's undefined, but I can print the whole array and see the IDs. Do I need to do localStorage or something differently or is there a way to work in native JavaScript variables?

Brian Ko
  • 163
  • 1
  • 8
  • 2
    Since `chrome.tabs.query` takes a callback, it's safe to assume it calls the callback asynchronously, and the `tabID` array is empty until then. – Jordan Running Aug 27 '18 at 21:08
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Jordan Running Aug 27 '18 at 21:08

0 Answers0