Skip to content

Instantly share code, notes, and snippets.

@lincore81
Created June 3, 2016 12:34
Show Gist options
  • Save lincore81/9bc9d5abf58b7d05ac1866b77ddc977b to your computer and use it in GitHub Desktop.
Save lincore81/9bc9d5abf58b7d05ac1866b77ddc977b to your computer and use it in GitHub Desktop.
// initial: value to call first task with
// tasks: varargs, functions to be called in order
// each task must take a callback function and optionally the result of the previous task
// the task must either call callback (with a result) or throw an error.
function async(initial, tasks) {
if (arguments.length === 0) throw new Error('argument required');
var chain = Array.from(arguments).slice(1);
var head = -1;
var top = chain.length - 1;
callback(initial);
function callback(result) {
head++;
if (head > top) return;
var task = chain[head];
task(callback, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment