javascript callback not pushing data up to the top of the calls <p>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.</p> <p>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.</p> <p>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.</p> <p>Some guidance would be really appreciated.</p> <pre><code>(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 &amp;&amp; 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 &lt; 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); }); }); }()); </code></pre>