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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:21:48+00:00 2026-05-31T07:21:48+00:00

I have make some functions to retrieve data using the Github API. I have

  • 0

I have make some functions to retrieve data using the Github API. I have the callbacks in place to get the data but I am sure how to understand where a function exits and when I stops modifying things.

For example in the code below, in the first function, when the AJAX call is successful, the callback is executed in the second function where the data is manipulated. Does that mean the the return in the first function is not needed or used? And in the second function is the data used and pushed to the array and then the array returned or is it the other way around where the (empty) array is returned and then the callback does its thing.

I am ultimately trying to get the data from the callback into an object and return that filled object from the parent function.

function makeAJAXCall(hash, cb) {
    var returnedJSON, cb = cb, hash = hash;
    $.ajax({
        accepts: 'application/vnd.github-blob.raw',
        dataType: 'jsonp',
        url: hash,
        success: function (json) {
            console.info(json);
            returnedJSON = json;

            // Time for callback to be executed
            if (cb) {
                cb(json);
            }
        },
        error: function (error) {
            console.error(error);
            // an error happened, check it out.
            throw error;
        }
    });
    return returnedJSON;
}

function parseBlob(hash) {
    var objectedJSON, objectList = [], i;
    objectedJSON = makeAJAXCall(hash, function (objectedJSON) {  // no loop as only one entry
        objectList.push(objectedJSON.content);
    });
    return objectList;
}

function walkTree(hash) {
    var objectedJSON, objectList = [], i, entry;
    var hash = 'https://api.github.com/repos/myAccountName/repo/git/trees/' + hash;
    objectedJSON = makeAJAXCall(hash, function (objectedJSON) {
        for (i = 0;  i < objectedJSON.data.tree.length; i += 1) {
            entry = objectedJSON.data.tree[i];
            console.debug(entry);
            if (entry.type === 'blob') {
                if (entry.path.slice(-4) === '.svg') {     // we only want the svg images not the ignore file and README etc
                    console.info(entry.path)
                    objectList.push(parseBlob(entry.url));
                }
            } else if (entry.type === 'tree') {
                objectList.push(walkTree(entry.sha));
            }
        }

    });
    console.info(objectList);
    return objectList;
}

$(document).ready(function () {
    var objects = walkTree('master', function () {     // master to start at the top and work our way down
        console.info(objects);
    });
});
  • 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-31T07:21:50+00:00Added an answer on May 31, 2026 at 7:21 am

    Here you are making an AJAX call A refers to asynchronous, ie your success/error callback will be executed asynchronously.

    makeAJAXCall will return before executing success/error of $ajax.

    so the objectedJSON = makeAJAXCall will return you undefined

    function makeAJAXCall(hash, cb) {
        $.ajax({
            accepts: 'application/vnd.github-blob.raw',
            dataType: 'jsonp',
            url: hash,
            success: function (json) {
                // this function will be executed after getting response from server 
                //ie Asynchronously
                //here cb passed from the makeAjaxCall exist in the closure scope
                if (cb) {
                    cb(json);
                }
            },
            error: function (error) {
                console.error(error);
                // an error happened, check it out.
                throw error;
            }
        });
    }
    

    Now when you call makeAjaxCall the callback function you are passing will exist in the closure scope of $.ajax and will be executed on success of server response

    makeAJAXCall(hash, function (objectedJSON) {  
        //objectJSON contains the response from server
        // do all your operations using server response over here or assign it to a global variable
    });
    

    check below links

    https://developer.mozilla.org/en/JavaScript/Guide/Closures

    https://mikewest.org/2009/05/asynchronous-execution-javascript-and-you

    or you can make your ajax call in sync using async:false which is highly not recommended

    function makeAJAXCall(hash, cb) {
        var returnedJSON;
        $.ajax({
            accepts: 'application/vnd.github-blob.raw',
            dataType: 'json',
            async : false, //this will make it in sync
            url: hash,
            success: function (json) {
                console.info(json);
                returnedJSON = json;
            //now makeAJAXCall will wait for success to complete and it will return only after executing success/error 
            // Time for callback to be executed
                if (cb) {
                    cb(json);
                }
            },
            error: function (error) {
                console.error(error);
                // an error happened, check it out.
                throw error;
            }
        });
        //will wait for success/error before returning
        return returnedJSON;   
    }
    

    In the above case your code will work

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

Sidebar

Related Questions

I have created some documents and managed to make some simple queries but I
I have an event handler, but I want to make sure that it will
Via ajax i retrieve some json data, make it as html and append it
I have to make some mods to an existing (and working) magento site. So
i have been programing in vc++ for sometime and i want to make some
I have bat-file, that make some operations. How to run this file from Delphi
I have a small app, where user can make some calculations and solve equations.
Welcome, I notice that Youtube make some changes into their website code. Anyone have
I need to make use of some OWL ontologies in c#. Does anyone have
I have some PHP code that is designed to make a spreadsheet with formulas

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.