So I am trying to create a bunch of tabs from a list of tabs in a list names groups like this..
console.log("groups.length: " + groups.length);
// loop through groups
for (var i = 0; i < groups.length; i++) {
// create the new tab
console.log("for loop ran: " + i);
// cannot use i in here because async
chrome.tabs.create({
windowId: win.id,
index: i,
url: groups[i].urls[0]
}, function(tab){
console.log("i: " + i);
groups[i].tabId = tab.id; // this is not working right!!!
});
}
So in the terminal I get..
groups.length: 2
popup.js:87 for loop ran: 0
popup.js:87 for loop ran: 1
popup.js:95 i: 2
popup.js:95 i: 2
Can someone explain to me what is going on here. I thought that even though the task is async it should still input the value of i as it was when the call was first made. Also why would line 95 ever show i = 2? How could i ever be greater than 1? I'm really confused, thanks kind internet programmers.