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 8572207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:57:23+00:00 2026-06-11T18:57:23+00:00

In my chrome extension i was checking for a function which can stop my

  • 0

In my chrome extension i was checking for a function which can stop my for loop from processing till it gets a response from content scripts. Sharing the sample code below

function abc() {
  chrome.tabs.query({'status': 'complete'}, function(tabArray) {
    for (var i = 0, tab; tab = tabArray[i]; i++) {
      var currentUrl = tab.url;
      var tabId = tab.id;
      if (currentUrl.match(otherthing)) {
        chrome.tabs.sendRequest(tabId, {'type': 'getrequiredthing'},
            function(response) {
              if (response.isrequiredthind) {
                callfunction(tabId);
              }
            }
        ); 
      }
    }
  });
}

Here when i get the matching url in else if i’m sending a request to the page for getting some info, if my info is positive i need to callfunction. But here in the for loop tabId is iterating very fastly and even if the response is positive it is calling the callfunction with next(or next) tabId.

Can you give your opinions on solving this keep waiting the for loop this response is received.

Thanks

  • 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-11T18:57:25+00:00Added an answer on June 11, 2026 at 6:57 pm

    The problem is that the third argument to sendRequest does not block on the request being ready. By design, JavaScript almost never blocks. This is a Good Thing. Instead, it uses an “event-driven” model.

    Another problem is due to lexical scoping: When callfunction is called, tabId has the most recent value, not the value when sendRequest was called. To get around this, you need to create a separate scope for each loop iteration e.g.

    for (...) {
      var tabId = ...;
      if (...) {
        (function (localTabId) {
          chrome.tabs.SendRequest(..., function (response) {
            if (response.isrequiredthind) {
              callfunction(localTabId);
            }
          }
        })(tabId);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a chrome extension that is a 'content script' I want to
I have a chrome extension which makes some changes on the site (editing comments).
I'm developing a Chrome extension for a client which want me to embed a
I am creating a Chrome extension which will send the URL of every page
I have a chrome extension which monitors the browser in a special way, sending
I am developing a chrome extension (my fourth question or so...) for facebook, which
I'm building a Chrome extension, which uses multiple APIs. Currently I have it set
background.js : chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { console.log(request.filter) chrome.webRequest.onBeforeRequest.addListener(request.func, request.filter, [blocking]); } ); content
I was creating a chrome extension in which some icons are shown in a
I'm writing a Chrome Extension that changes calls to document.all so that it can

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.