0

Here is the code:

var newTab;
chrome.tabs.create({url: (newUrl)}, function(tab){
  newTab = tab.id;
});
check();
alert(newTab);
chrome.tabs.update(newTab, {pinned: true});
chrome.tabs.update(tabid, {active: true});
chrome.tabs.remove(newTab);

The final instruction doesn't work. the newTab variable doesn't update so that is can be pinned and then remove.

Jon
  • 29
  • 2
  • 7
  • 2
    possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – levi Feb 01 '15 at 00:01

1 Answers1

2

Try:

var newTab;
chrome.tabs.create({url: (newUrl)}, function(tab){
  newTab = tab.id;

  check();
  alert(newTab);
  chrome.tabs.update(newTab, {pinned: true});
  chrome.tabs.update(tabid, {active: true});
  chrome.tabs.remove(newTab);
});

// Code here is run before the tab is created.

The reason for this is that the create method on the tabs object is asynchronous. This means that any code after the method invocation will be run before the new tab has been created, causing the error.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331