Created
February 29, 2012 07:16
-
-
Save nicokruger/1938816 to your computer and use it in GitHub Desktop.
synchelper
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
| var _ = require("underscore"); | |
| /** | |
| * First argument is the callback to call when all are done | |
| * all other callbacks are functions of the form | |
| * function (callback, ...) { } | |
| * and must call callback when they are done | |
| * the ... will be any additional parameters from the previous step as | |
| * the first parameter of all functions is partially applied to be the callback | |
| * on creation of sequence. | |
| */ | |
| var waitForAll = function (/*...*/) { | |
| var doneCallback = _.first(arguments), | |
| actions, | |
| callback = function () { | |
| if (actions.length) { | |
| actions.shift().apply(null,arguments); | |
| } else { | |
| doneCallback(); | |
| } | |
| }; | |
| actions = _.chain(arguments).tail().map(function (f) { | |
| return _.bind(f, null, callback); | |
| }).value(); | |
| callback(); | |
| }; | |
| exports.waitForAll = waitForAll; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment