Skip to content

Instantly share code, notes, and snippets.

@Mehuge
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mehuge/4ea8e9deb8bae3774b59 to your computer and use it in GitHub Desktop.
Save Mehuge/4ea8e9deb8bae3774b59 to your computer and use it in GitHub Desktop.
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();
});
});
}
@Mehuge
Copy link
Author

Mehuge commented Aug 22, 2015

Works for sync or async tasks, or even a mix of both

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment