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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:22:49+00:00 2026-05-31T12:22:49+00:00

I am slowly learning how to use callbacks and I am running into trouble.

  • 0

I am slowly learning how to use callbacks and I am running into trouble. I think the code below should work byt it is not.

From what I can tell it is not descending in to the recursive function as when it hit another ‘tree’ entry or outputting anything at the end.

One of the issues I ran into is the callback in the for loop. I was not sure that there is a better way to do it: I just used a counter to see if I was at the end of the loop before calling the callback.

Some guidance would be really appreciated.

(function () {
    'use strict';

    var objectsList = [];

    function makeAJAXCall(hash, cb) {
        $.ajaxSetup({
            accept: 'application/vnd.github.raw',
            dataType: 'jsonp'
        });

        $.ajax({
            url: hash,
            success: function (json) {

                if (cb) {
                    cb(json);
                }
            },
            error: function (error) {
                console.error(error);
                throw error;
            }
        });
    }

    function parseBlob(hash, cb) {
        makeAJAXCall(hash, function (returnedJSON) {  // no loop as only one entry
            if (cb) {
                cb(returnedJSON.data);
            }
        });
    }

    function complete(cb, loopLength, treeContents) {
        concole.info(loopLength);
        if (cb && loopLength === 0) {
            objectsList.push(treeContents);
            cb();
        }
    }

    function parseTree(hash, treeName, cb) {
        var treeContents = {'tree': treeName, 'blobs': []}, loopLength, i, entry;
        var tree = 'https://api.github.com/repos/myusername/SVG-Shapes/git/trees/' + hash;
        makeAJAXCall(tree, function (returnedJSON) {
            loopLength = returnedJSON.data.tree.length;
            for (i = 0;  i < returnedJSON.data.tree.length; i += 1) {
                entry = returnedJSON.data.tree[i];
                if (entry.type === 'blob') {
                    if (entry.path.slice(-4) === '.svg') {     // we only want the svg images not the ignore file and README etc
                        parseBlob(entry.url, function (json) {
                            treeContents.blobs.push(json.content);
                            loopLength -= 1;
                            complete(hash, loopLength, cb);
                        });
                    }
                } else if (entry.type === 'tree') {
                    parseTree(entry.sha, entry.path, function () {console.info(objectsList);});
                }
            }
        });
    }

    $(document).ready(function () {
        parseTree('master', 'master', function () {     // master to start at the top and work our way down
            console.info(objectsList);
        });
    });
}());
  • 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-31T12:22:51+00:00Added an answer on May 31, 2026 at 12:22 pm

    Your recursion will not work properly because the variable where you are collecting blob data treeContents is a local variable of the recursive function so it’s created new and destroyed on every call to parseTree(). It will not be accumulating data. You must create this variable outside the scope of the parseTree() function so a single instance of this variable can live from one call to the next and you can correctly accumulate data in it.

    There are several ways to fix this:

    1. You can pass the current state of treeContents into the parseTree() function
    2. You can make the recursive part of the function be a local function that shares a common treeContents variable.
    3. You can make the treeContents variable be a global variable.

    The second is my choice here something like this:

    function parseTree(topHash, topTreeName, topCb) {
        var treeContents = {'tree': toptreeName, 'blobs': []};
    
        function parse(hash, treeName, cb) {
            var loopLength, i, entry;
            var tree = 'https://api.github.com/repos/myusername/SVG-Shapes/git/trees/' + hash;
            makeAJAXCall(tree, function (returnedJSON) {
                loopLength = returnedJSON.data.tree.length;
                for (i = 0;  i < returnedJSON.data.tree.length; i += 1) {
                    entry = returnedJSON.data.tree[i];
                    if (entry.type === 'blob') {
                        if (entry.path.slice(-4) === '.svg') {     // we only want the svg images not the ignore file and README etc
                            parseBlob(entry.url, function (json) {
                                treeContents.blobs.push(json.content);
                                loopLength -= 1;
                                complete(hash, loopLength, cb);
                            });
                        }
                    } else if (entry.type === 'tree') {
                        parse(entry.sha, entry.path, function () {console.info(objectsList);});
                    }
                }
            });
        }
    
        parse(topHash, topTreeName, topCb);
    }
    

    Assuming the ajax call is asynchronous, you will still need to find a way to know when you’re done with all the parsing and call some function that you pass the treeContents data to because otherwise, that data is not available to any other function. It can’t be simply returned from parseTree because of the asynch nature of the ajax calls.

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

Sidebar

Related Questions

I am learning Java (slowly) from the ground up, but every now and again
Possible Duplicate: MVC(3) handleUpdate I'm (slowly) learning how to use MVC 3 and at
I'm really new to java and slowly learning so not sure if there is
I'm just now getting into jquery and slowly learning it (shame, I know), and
I am slowly learning how to use Reactive Extensions for .NET with WPF. There
I'm slowly working through an Android learning book and was given the following code
I am just learning how to use ScriptingBridges. I made a method that slowly
I'm slowly learning SQL and how to use form builder 6. The situation is
I'm slowly learning how to apply CakePHP's Translate Behavior and I think I've grasped
I am slowly learning how to modify and write jQuery from scratch and have

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.