Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow for more than one step, like npm does

  • Loading branch information...
commit 9be42103f41062a9ebe6aca82373a861438d76da 1 parent 7cc9247
@isaacs isaacs authored
Showing with 50 additions and 11 deletions.
  1. +50 −11 lib/async-map.js
View
61 lib/async-map.js
@@ -1,17 +1,56 @@
+
+/*
+usage:
+
+// do something to a list of things
+asyncMap(myListOfStuff, function (thing, cb) { doSomething(thing.foo, cb) }, cb)
+// do more than one thing to each item
+asyncMap(list, fooFn, barFn, cb)
+
+*/
+
module.exports = asyncMap
-function asyncMap (list, fn, cb_) {
- var n = list.length
- , results = []
+
+function asyncMap () {
+ var steps = Array.prototype.slice.call(arguments)
+ , list = steps.shift() || []
+ , cb_ = steps.pop()
+ if (typeof cb_ !== "function") throw new Error(
+ "No callback provided to asyncMap")
+ if (!list) return cb_(null, [])
+ if (!Array.isArray(list)) list = [list]
+ var n = steps.length
+ , data = [] // 2d array
, errState = null
- function cb (er, data) {
+ , l = list.length
+ , a = l * n
+ if (!a) return cb_(null, [])
+ function cb (er) {
if (errState) return
- if (er) return cb_(errState = er)
- results.push(data)
- if (-- n === 0)
- return cb_(null, results)
+ var argLen = arguments.length
+ for (var i = 1; i < argLen; i ++) if (arguments[i] !== undefined) {
+ data[i - 1] = (data[i - 1] || []).concat(arguments[i])
+ }
+ // see if any new things have been added.
+ if (list.length > l) {
+ var newList = list.slice(l)
+ a += (list.length - l) * n
+ l = list.length
+ process.nextTick(function () {
+ newList.forEach(function (ar) {
+ steps.forEach(function (fn) { fn(ar, cb) })
+ })
+ })
+ }
+
+ if (er || --a === 0) {
+ errState = er
+ cb_.apply(null, [errState].concat(data))
+ }
}
- if (list.length === 0) return cb_(null, [])
- list.forEach(function (l) {
- fn(l, cb)
+ // expect the supplied cb function to be called
+ // "n" times for each thing in the array.
+ list.forEach(function (ar) {
+ steps.forEach(function (fn) { fn(ar, cb) })
})
}
Please sign in to comment.
Something went wrong with that request. Please try again.