Created
November 5, 2010 19:47
-
-
Save drewlesueur/664673 to your computer and use it in GitHub Desktop.
Javascript pattern for avoiding nested callbacks
This file contains hidden or 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
# Drew LeSueur @drewlesueur | |
# An abstraction for calling multiple asynchronous | |
# functions at once, and calling a callback | |
# with the "return values" of all functions | |
# when they are all done. | |
# requires underscore.js | |
_.mixin # underscore.js mixin | |
do_these: (to_dos, callback) -> | |
return_values = if _.isArray(to_dos) then [] else {} | |
make_jobs_done = (id) -> | |
return (ret) -> | |
return_values[id] = ret | |
all_done = true | |
_.each to_dos, (func, id) -> | |
if not(id of return_values) | |
all_done = false | |
_.breakLoop() | |
if all_done is true | |
callback(return_values) | |
_.each to_dos, (to_do, id) -> | |
jobs_done = make_jobs_done(id) | |
to_do(jobs_done) | |
## Example usage | |
# get_pics = (done) -> | |
# API.getPictures "houses", (urls) -> | |
# done urls | |
# | |
# get_videos = (done) -> | |
# API2.login, "user", "password", (access) -> | |
# access.getVideos (videos) -> | |
# done videos | |
# | |
# _.do_these [get_pics, get_videos], (ret) -> | |
# console.log "pics are", ret[0] | |
# console.log "videos are", ret[1] | |
# | |
## OR | |
# | |
# _.do_these {pics: get_pics, videos: get_videos}, (ret) -> | |
# console.log "pics are ", ret.pics | |
# console.log "videos are", ret.videos | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might also like promises: http://www.sitepen.com/blog/2010/09/20/promised-io/