Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created September 29, 2012 02:19
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 Raynos/3802935 to your computer and use it in GitHub Desktop.
Save Raynos/3802935 to your computer and use it in GitHub Desktop.
function mapAsync(iterator) {
var counter = 0
, ended = false
, queue = ReadWriteStream(write, end)
, stream = queue.stream
return stream
function write(chunk, queue) {
counter++
iterator(chunk, next)
}
function next(err, data) {
if (err) {
return stream.emit("error", err)
}
queue.push(data)
counter--
if (counter === 0 && ended) {
queue.end()
}
}
function end() {
ended = true
if (counter === 0) {
queue.end()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment