'Tab in undefined when Chrome Extension console is opened and focused

I have this function :

async getCurrentTab() {
    let queryOptions = { active: true, currentWindow: true };
    let [tab] = await chrome.tabs.query(queryOptions);
    console.log("current tab: " + tab);
    return tab;
}

which is triggered this way :

chrome.tabs.onUpdated.addListener(changePageHandler);
    async function changePageHandler(tabId, changeInfo) {
        if (changeInfo.status == 'complete') {
            getCurrentTab();
        }
    }

My problem is that when i focus the Chrome Console window (click or just CMD+TAB to it) and then refresh the web page i get this :

current tab: undefined

But if I click on the webpage and refresh or just close the Chrome Console window i get this :

current tab: [object Object]

Which means (I think) that it has been recognized, can somebody explain to me what is going on please ?

Thanks !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source