Skip to content

Instantly share code, notes, and snippets.

@pateketrueke
Last active August 29, 2015 14:06
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 pateketrueke/96c0c13ee6d26ee2cda5 to your computer and use it in GitHub Desktop.
Save pateketrueke/96c0c13ee6d26ee2cda5 to your computer and use it in GitHub Desktop.
function runTasks(subtasks, callback) {
function next() {
var task = subtasks.shift();
if (!task) {
return callback();
}
task(function (err) {
if (err) {
return callback(err);
}
next();
});
}
next();
}
var all = [];
[1,2].forEach(function () {
all.push(function (next) {
console.log('OPEN');
setTimeout(function () {
console.log('CLOSE');
next();
}, 2000);
});
});
runTasks(all, function (err) {
console.log("DONE'EM ALL");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment