Created
June 3, 2016 12:34
-
-
Save lincore81/9bc9d5abf58b7d05ac1866b77ddc977b to your computer and use it in GitHub Desktop.
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
// 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