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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:15:54+00:00 2026-06-10T22:15:54+00:00

This is super simple task to do in Java but the asynchronous nature of

  • 0

This is super simple task to do in Java but the asynchronous nature of javascript makes this task(for me) almost impossible, at least with my knowledge now.(I’m not trying to bash javascript. Love the language!).

It’s very basic. A top level tree has a parent of null in my mysql database. It’s easy finding children. The children have lines available to them. The depth of the tree is variable.

    private static Set<Tree> getBranches( Tree trunk ) {

    Set<Tree> treeSet = new HashSet<Tree>();

    if ( trunk != null ) {

        if ( trunk.hasLines() ) { //queries if tree has lines.  returns true or false
            treeSet.add( trunk );
        }

        for ( Tree tree : trunk.treeList ) {
            treeSet.addAll( getBranches( tree ) );
        }
    }

    return treeSet;
}

Basically the method tests if the tree has lines available. If it does it adds all of those to a set. If not it continues until it finds lines.

The asynchronous nature of the mysql node library turns this task into hell.

Here is what I have now

   function hasLines(tree_id, callback) {
        var ret;
        pool.query('SELECT * from pkg_line_tree where tree_id = ?', [tree_id], function (err, rows) {

            if (rows.length > 0) {
                ret = true;
            } else {
                ret = false;
            }
            callback(ret);
        });
    }


     function dig(tree_id, treeArray, callback) {

        pool.query('SELECT * from tree where parent_id = ?', [tree_id], function (err, rows) {

           if (rows) {

              for (var i in rows) {
                 hasLines(rows[i].tree_id, function (t) {

                    if (t) {
                       treeArray.push(rows[i].tree_id);
                    } else {
                       treeArray.concat(dig(rows[i].tree_id, treeArray));
                    }
                 });
              }

              if (callback) {
                 callback(treeArray);
              }

           }
        });

        return treeArray;
     }


     var treeArray = [];
     dig(52, treeArray, function (t) {
        res.json(t);
     });

I really just need to output all the children available in this root tree.

Please let me know if this doesn’t make sense. I’ll try to refactor. I’m hoping I got some kind of point across. I’d hate to use something like Fibers to get this done but I’m out of options. 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-10T22:15:55+00:00Added an answer on June 10, 2026 at 10:15 pm

    Your use of dig() isn’t currently consistent:

    // asynchronous with callback
    dig(52, treeArray, function (t) {
       res.json(t);
    });
    
    // then synchronous with `return`?
    treeArray.concat(dig(rows[i].tree_id, treeArray));
    

    Also, the concat in the last line isn’t actually doing much, since it doesn’t alter the array it’s called on. You probably wouldn’t actually want it to as dig passes around the treeArray rather than defining a new treeSet like in getBranches. So, if it did, it would append treeArray onto the end of itself each time.

    You could still use concat with multiple treeSets, but you’d have to store its return value:

    treeSet = treeSet.concat(subSet);
    

    And, you’ll have to replace the for loop this with an asynchronous iterator as the loop won’t wait for asynchronous operations before continuing. The async library has a few options for this, if you’re up for trying it.

    So, with multiple treeSets, concat, and async.forEachSeries, you could try:

    function dig(tree_id, callback) {
      var treeSet = [];
    
      hasLines(tree_id, function (yep) {
        if (yep) {
          treeSet.push(tree_id);
        }
    
        pool.query('SELECT * from tree where parent_id = ?', [tree_id], function (err, rows) {
    
          function each(row, next) {
            dig(row.tree_id, function (subSet) {
              treeSet = treeSet.concat(subSet);
              next(null);
            });
          }
    
          function done() {
            callback(treeSet);
          }
    
          async.forEachSeries(rows, each, done);
        });
      });
    }
    
    dig(52, function (treeSet) {
      res.json(treeSet);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be super simple. The first code block works, but when I wrap
This may be super simple - but I'm struggling to spot what's going on.
I'm using this code for my web assignment: http://csstechniques.blogspot.sg/2007/05/super-simple-css-bars.html But how do I show
I need a super-readable version of this super simple inheritance in JavaScript. This is
This should be super simple, but I'm not sure why the compiler is complaining
Seems like this should be super-simple but looking around for a simple (or half-way
This might be a super simple bug, but my eyes are practically bleeding from
I know that this is supposedly a super simple question, but I've been struggling
I bet this is super simple and easy but can't find a basic example
I Know this is super simple but for the life of me i cant

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.