I'm a beginner in JS and Chrome extensions as I came there from the PHP-world. So I have a piece of code in my background.js:
function findAtab(regex){
var t = false;
alert('t1: '+t);
chrome.tabs.getAllInWindow(function(tabs){
tabs.forEach(function(tab){
if (regex.test(tab.url)) {
t = tab.id;
alert('t2: '+t);
}
});
});
alert('t3: '+t);
return t;
}
var someRegex = /^https?:\/\/(?:[^./?#]+\.)?somesite\.com/;
var theTab = findAtab(someRegex);
Now when I run the code it shows me t1: false, then t3: false, and finally t2: 1060 (I have a tab with a compatible site opened). Why is that? As I understand it has something to do with the way JS executes code, (singlethreaded?), but where to dig?