Last active
August 29, 2015 14:27
-
-
Save Mehuge/4ea8e9deb8bae3774b59 to your computer and use it in GitHub Desktop.
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
function doThings(todo, whenDone, thereWereErrors) { | |
var tasks = todo.length, errors = 0; | |
function finish() { | |
task --; | |
if (task === 0) { | |
// everything is done | |
if (errors) { | |
// but there were errors | |
thereWereErrors(); | |
} else { | |
// do the thing that depends on everything being done | |
whenDone(); | |
} | |
} | |
} | |
todo.forEach(function(thing) { | |
// start in this example returns a promise | |
thing.start() | |
.then(function() { | |
// thing has finished | |
finish(); | |
}, | |
function(reason) { | |
// thing errored | |
errors ++; | |
finish(); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works for sync or async tasks, or even a mix of both