Javascript chrome.tabs.query中的push()';不能正确地添加到数组中

Javascript chrome.tabs.query中的push()';不能正确地添加到数组中,javascript,arrays,google-chrome,Javascript,Arrays,Google Chrome,所以,我有一个问题,我不能完全明白自己,即使经过几个小时的尝试和错误,当然,在这个网站上的多次查询 我基本上是想用Chrome中打开的所有选项卡的标题填充一个数组,但请自己看看: var children = []; function updateList(){ chrome.tabs.query({}, function(tabs){ for( i = 0; i < tabs.length; i++){ children.push(tab

所以,我有一个问题,我不能完全明白自己,即使经过几个小时的尝试和错误,当然,在这个网站上的多次查询

我基本上是想用Chrome中打开的所有选项卡的标题填充一个数组,但请自己看看:

var children = [];

function updateList(){
    chrome.tabs.query({}, function(tabs){
        for( i = 0; i < tabs.length; i++){
            children.push(tabs[i].title);
        }
    })
    children.push("test")
}


console.log(children);
console.log(children.length);
updateList();
console.log(children);
console.log(children.length);
我不知道这是什么原因,但我希望这里的人能告诉我是什么原因


编辑: 好吧,出于某种原因,我没有尝试在Chrome控制台中手动检查阵列的长度,但我现在已经这样做了。长度是正确的,因此查询似乎花费的时间太长,无法生效。现在的问题是:如何强制程序等待查询结束?

是异步的,这就是它需要回调的原因。要确保
查询
回调完成,请将您自己的添加到
更新列表
,如下所示:

var children = [];

function updateList(callback){
    chrome.tabs.query({}, function(tabs){
        for( i = 0; i < tabs.length; i++){
            children.push(tabs[i].title);
        }

        children.push("test")
        if (callback) {
          console.log('calling updateList callback');
          callback();
        }
    })
}


console.log(children);
console.log(children.length);
console.log('calling updateList')
updateList(function() {
  console.log('in updateList callback');
  console.log(children);
  console.log(children.length);
});
console.log('called updateList');
var children=[];
函数updateList(回调){
chrome.tabs.query({},函数(tabs){
对于(i=0;i
var children = [];

function updateList(callback){
    chrome.tabs.query({}, function(tabs){
        for( i = 0; i < tabs.length; i++){
            children.push(tabs[i].title);
        }

        children.push("test")
        if (callback) {
          console.log('calling updateList callback');
          callback();
        }
    })
}


console.log(children);
console.log(children.length);
console.log('calling updateList')
updateList(function() {
  console.log('in updateList callback');
  console.log(children);
  console.log(children.length);
});
console.log('called updateList');