Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:26:22+00:00 2026-06-08T18:26:22+00:00

I’m creating (learning) an extension for Google Chrome. To debug some code, I inserted

  • 0

I’m creating (learning) an extension for Google Chrome.

To debug some code, I inserted console.log(), as follows:

var fourmTabs = new Array();
chrome.tabs.query({}, function (tabs) {
    for (var i = 0; i < tabs.length; i++) {
        fourmTabs[i] = tabs[i];
    }
});
for (var i = 0; i < fourmTabs.length; i++) {
    if (fourmTabs[i] != null)
        window.console.log(fourmTabs[i].url);
    else {
        window.console.log("??" + i);
    }
}

It’s very simple code: get all tabs info into an array of my own, and print some things.

To check whether the code works as it should, I run the code. Here comes the problem:

  • When I use breakpoints (via the Developer tools), the code runs fine.
  • Without breakpoints, nothing is printed.

Any idea why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:26:23+00:00Added an answer on June 8, 2026 at 6:26 pm

    Your problem can be simplified to:

    /*1.*/ var fourmTabs = [];
    /*2.*/ chrome.tabs.query({}, function(tabs) {
    /*3.*/     fourmTabs[0] = tabs[0];
    /*4.*/ });
    /*5.*/ console.log(fourmTabs[0]);
    

    You expect that the fourmTabs array is updated (by line 3) when line 5 is reached.
    That is wrong, because the chrome.tabs.query method is asynchronous.


    In an attempt to make you understand the significance of the asynchronous aspect, I show a code snippet with the same structure as your code and a story.

    /*1.*/ var rope = null;
    /*2.*/ requestRope(function(receivedRope) {
    /*3.*/     rope = receivedRope;
    /*4.*/ });
    /*5.*/ grab(rope);
    
    • At line 1, the presence of a rope is announced.
    • At lines 2-4, a callback function is created, which ought to be called by the requestRope function.
    • At line 5, you’re going to grab the rope via the grab function.

    When requestRope is implemented synchronously, there’s no problem:
      You: “Hi, I want a rope. Please throw the rope“call the callback function” when you’ve got one.”
      She: “Sure.” throws rope
      You: Jumps and grabs rope – You manage to get at the other side, alive.

    When requestRope is implemented asynchronously, you may have a problem if you treat it as synchronous:
      You: “Please throw a rope at me.”
      She: “Sure. Let’s have a look…”
      You: Jumps and attempts to grab rope Because there’s no rope, you fall and die.
      She: Throws rope Too late, of course.


    Now you’ve seen the difference between an asynchronously and synchronously implemented function, let’s solve your original question:

    var fourmTabs = new Array();
    chrome.tabs.query({}, function (tabs) {
        for (var i = 0; i < tabs.length; i++) {
            fourmTabs[i] = tabs[i];
        }
        // Moved code inside the callback handler
        for (var i = 0; i < fourmTabs.length; i++) {
            if (fourmTabs[i] != null)
               window.console.log(fourmTabs[i].url);
            else {
                window.console.log("??" + i);
            }
        }
    });
    // <moved code inside callback function of chrome.tabs.query>
    

    With breakpoints, your code works, because by the time that the second part of the code is reached, the callback has already been called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.