Created
September 25, 2011 16:07
-
-
Save lucagrulla/1240766 to your computer and use it in GitHub Desktop.
Marks-solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for the origina blog post check | |
// http://www.markhneedham.com/blog/2011/09/25/jquery-collecting-the-results-from-a-collection-of-asynchronous-requests/ | |
var people = ["Marc", "Liz", "Ken", "Duncan", "Uday", "Mark", "Charles"]; | |
asyncLoop(people, [], function(name, grid, callBackFn) { | |
// parse data and create something cool | |
grid.push(somethingCool); | |
callBackFn(); | |
}, function(grid) { | |
// do something with grid | |
}); | |
function asyncLoop(collection, seedResult, loopFn, completionFn) { | |
var copy = collection.slice(0); | |
(function loop() { | |
var item = copy.shift(); | |
if(copy.length == 0) { | |
completionFn(seedResult); | |
} else { | |
loopFn(item, seedResult, loop); | |
} | |
})(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment