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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:54:58+00:00 2026-05-28T20:54:58+00:00

I need to visit each node in a tree, do some asynchronous work, and

  • 0

I need to visit each node in a tree, do some asynchronous work, and then find out when all of the asynchronous work has completed. Here are the steps.

  1. Visit a node and modify its children asynchronously.
  2. When async modifications to children are done, visit all children (which might require async work).
  3. When all asynchronous work for all descendants is done, do something else.

Update:

I ended up using a pattern that looks like a monitor/lock (but isn’t) for each node to know when to begin step 2. I used events and attributes to keep track of all descendants of a node to know when to begin step 3.

It works, but man is this difficult to read! Is there a cleaner pattern?

function step1(el) { // recursive
  var allDone = false;
  var monitor = new Monitor();
  var lock = monitor.lock(); // obtain a lock
  $(el).attr("step1", ""); // step1 in progress for this node

  // fires each time a descendant node finishes step 1
  $(el).on("step1done", function (event) {
    if (allDone) return;
    var step1Descendants = $(el).find("[step1]");
    if (step1Descendants.length === 0) {
      // step 1 done for all descendants (so step 2 is complete)
      step3(el); // not async
      allDone = true;
    }
  });

  // fires first time all locks are unlocked
  monitor.addEventListener("done", function () {
    $(el).removeAttr("step1"); // done with step 1
    step2(el); // might have async work
    $(el).trigger("step1done");
  });

  doAsyncWork(el, monitor); // pass monitor to lock/unlock
  lock.unlock(); // immediately checks if no other locks outstanding
};

function step2(el) { // visit children
  $(el).children().each(function (i, child) {
    step1(child);
  });
};
  • 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-28T20:54:59+00:00Added an answer on May 28, 2026 at 8:54 pm

    It seems the right pattern for this problem and for async work in general is Promises. The idea is that any function that will do asynchronous work should return a promise object, to which the caller can attach functions that should be called when the asynchronous work is completed.

    jQuery has a great API for implementing this pattern. It’s called a jQuery.Deferred object. Here’s a simple example:

    function asyncWork() {
      var deferred = $.Deferred();
      setTimeout(function () {
        // pass arguments via the resolve method
        deferred.resolve("Done.");
      }, 1000);
      return deferred.promise();
    }
    
    asyncWork().then(function (result) {
      console.log(result);
    });
    

    Very tidy. What’s the difference between a Deferred object and its promise object? Good question.

    Here’s how you might apply this pattern to solve this problem.

    function step1(el) { // recursive
      var deferred = $.Deferred();
    
      // doAsyncWork needs to return a promise
      doAsyncWork(el).then(function () {
        step2(el).then(function () {
          step3(el); // not async
          deferred.resolve();
        });
      });
      return deferred.promise();
    };
    
    function step2(el) { // visit children
      var deferred = $.Deferred();
      var childPromises = [];
      $(el).children().each(function (i, child) {
        childPromises.push(step1(child));
      });
    
      // When all child promises are resolved…
      $.when.apply(this, childPromises).then(function () {
        deferred.resolve();
      });
      return deferred.promise();
    };
    

    So much cleaner. So much easier to read.

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

Sidebar

Related Questions

The problem I'm trying to solve concerns a tree of MRT system. Each node
I need to find the number of elements in a tree using an iterative
I need to visit a folder and all of its children with SSIS (SQL
I need to redirect a user to a different page if they visit a
Need to locate the following pattern: The letter I followed by a space then
need ask you about some help. I have web app running in Net 2.0.
Need some help about with Memcache. I have created a class and want to
Need to insert selected text on the page into textarea. There must be some
I am working on tab host application containing 4 tabs. Each tab has individual
On Google App Engine in Python, I have a Visit entity that has a

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.