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

  • Home
  • SEARCH
  • 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 6554999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:45:59+00:00 2026-05-25T12:45:59+00:00

I am working on a Javascript simulator that runs in a web browser. It

  • 0

I am working on a Javascript simulator that runs in a web browser. It has a main loop:

do {
    updateVisualization(simulator);
    simulator.doStep();
} while (!reachedGoal(simulator));

And for every iteration, I need to run several workers that must be concurrently executed:

doStep = function() {
    ...
    for (every agent in the simulation) {
        var worker = new Worker('worker.js');
        worker.onmessage = function(event) {
            ...
        }
        worker.postMessage(...);
    }

    // Here is the problem
}

My question is: how could I wait for every worker to finish?

  • 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-25T12:46:00+00:00Added an answer on May 25, 2026 at 12:46 pm

    A suggestion:

    doStep = function () {
        var i, worker;
    
        updateVisualization( simulator );    
    
        simulator.workers = []; // array of workers for this step
    
        for ( i = 0; i < agents.length; i++ ) {
            // set up new worker
            worker = new Worker( 'worker.js' );
            worker.onmessage = function ( e ) {
                var i;
    
                if ( e.data === 'finished' ) {
                    this.finished = true;
    
                    for ( i = 0; i < simulator.workers.length; i++ ) {
                        if ( !simulator.workers[i].finished ) {
                            return;
                        }
                    }
    
                    // survived for-loop = all workers finished
                    if ( !reachedGoal( simulator ) ) { // another iteration?
                        simulator.doStep();    
                    }
                }
            };
            worker.postMessage( 'doStep' );
    
            simulator.workers.push( worker ); // push worker into workers array
        }
    };
    

    So all the action happens in the onmessage callback of the workers. Every time a worker responds with a message, you inspect the simulator.workers array by checking if all workers have a finished property set to true. If that is the case, this means that all workers finished and you can move on (the “survived for-loop” part).

    So basically, you instantiate all workers and then just wait for their responses… no loop required.

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

Sidebar

Related Questions

I need JavaScript working almost perfectly in my application that should be able to
I am working on a javascript app that users load via a configurable script
I am working on a JavaScript utility that tells the user the number of
I am working on a Javascript object that contains some YUI objects. The key
I'm currently working on a JavaScript tool that, during the course of its execution,
I have some working Javascript that manipulates the some DOM elements. The problem is,
I'm a C/C++/Java programmer working with JavaScript. I'm trying to write a function that
I've been working on implementing a pretty complex system in JavaScript that needs to
Greetings, I've been working on a web-interface for some hardware that uses an 8-bit
I have some working Javascript code that generates an RDF/XML document using variables picked

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.