Created
April 30, 2013 02:07
-
-
Save Raynos/5486205 to your computer and use it in GitHub Desktop.
Continuable list
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
module.exports = list | |
// lists := (tasks:Array<Continuable<T>>) | |
// => Continuable<Array<T>> | |
function list(tasks) { | |
return function continuable(callback) { | |
var result = [] | |
var ended = false | |
var count = 0 | |
var length = tasks.length | |
tasks.forEach(function (source, index) { | |
source(function (err, value) { | |
if (err && !ended) { | |
callback(err) | |
} else if (!err) { | |
result[index] = value | |
if (++count === length) { | |
callback(null, result) | |
} | |
} | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment