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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:47:08+00:00 2026-05-29T20:47:08+00:00

I have a large script that is very synchronous. 1) it runs a function

  • 0

I have a large script that is very synchronous.

1) it runs a function like this:

var result = doSomethingSynchronous(myVar);
if (result === 'something') {
  doSomethingElse();
}

the doSomethingSynchronous function has a for loop that returns a value on a if statement:

var i = 0; len = someVariable.length;
for (; i < len; i++) {
  if (someVariable[i] === 'aString') {
    return true;
  } else {
    doSomethingElse();
  }
}

Because the for loop and the processing can be intense, I’ve replaced the for loop with an iterative queue processing function that runs itself asynchronously using setTimeout after shifting one element from the array. Once it runs asynchronously of course it doesn’t return the value and assigns it to the ‘result’ variable in the first snippet synchronously, and the if statement there always fails. My question is, is there a way to keep this part synchronous? Have the value be assigned to result before the script goes on to the next line? Or do I necessary have to use the observer/mediator pattern or similar to notify that the asynchronous function is completed?

Alternatively I could use a callback, but the actual real code is longer than this one and all depend on synchronicity. So switching var result = doSomethingSynchronous() to asynchronous would have a domino effect on the rest of the synchronous scripts, correct? 🙂
This sounds like a difficult and error-prone conversion, so I thought I’d ask for advice.

Thanks!

— UDPATED to fix variable name “le”. It should be “len”

  • 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-05-29T20:47:11+00:00Added an answer on May 29, 2026 at 8:47 pm

    In order to assign the result to a value, you would need to pass through a callback and call that instead of returning a value, whether or not you’re deferring functionality to a timer or not.

    function doSomething(someVariable, callback){
      var i = 0; len = someVariable.length;
      for (; i < len; i++) {
        if (someVariable[i] === 'aString') {
          callback && callback(true);
          break;
        } else {
          setTimeout(function(){ deferredSomething(callback); }, 10);
        }
      }
    }
    
    function deferredSomething(callback){
      /* code.. */
      callback && callback('something');
    }
    
    doSomething(myVar, function(result){
      if (result === 'something') {
        doSomethingElse();
      }
    });
    

    I’ll also mention that if you are doing something intense, you should definitely look at web workers so you can offload to a background thread, especially if you’re working with just a FireFox add-on. https://developer.mozilla.org/En/Using_web_workers

    –UPDATED TO FIX EXAMPLE CODE ( fixed variable “le”, should be “len” )

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

Sidebar

Related Questions

I have written a very complex database migration script in Groovy, that runs just
I have a very large script which contains a lot of php files, so
Hello I have a very large SQL script and while trying to execute it
I have a very large php maintenance script (basically it recreates thumbnails for an
I have a data processing system that generates very large reports on the data
I have a very painful slow script that gets lots of data from MySQL
I have a tab-delimited text file that is very large. Many lines in the
I have a very large database (with new information being added everyday) that a
I'm trying to write a script that breaks up a VERY large file into
I have a very large table (that overflows the content div) and because it's

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.