5 // do something to a list of things
6 asyncMap(myListOfStuff, function (thing, cb) { doSomething(thing.foo, cb) }, cb)
7 // do more than one thing to each item
8 asyncMap(list, fooFn, barFn, cb)
12 module.exports = asyncMap
14 function asyncMap () {
15 var steps = Array.prototype.slice.call(arguments)
16 , list = steps.shift() || []
18 if (typeof cb_ !== "function") throw new Error(
19 "No callback provided to asyncMap")
20 if (!list) return cb_(null, [])
21 if (!Array.isArray(list)) list = [list]
23 , data = [] // 2d array
27 if (!a) return cb_(null, [])
29 if (er && !errState) errState = er
31 var argLen = arguments.length
32 for (var i = 1; i < argLen; i ++) if (arguments[i] !== undefined) {
33 data[i - 1] = (data[i - 1] || []).concat(arguments[i])
35 // see if any new things have been added.
36 if (list.length > l) {
37 var newList = list.slice(l)
38 a += (list.length - l) * n
40 process.nextTick(function () {
41 newList.forEach(function (ar) {
42 steps.forEach(function (fn) { fn(ar, cb) })
47 if (--a === 0) cb_.apply(null, [errState].concat(data))
49 // expect the supplied cb function to be called
50 // "n" times for each thing in the array.
51 list.forEach(function (ar) {
52 steps.forEach(function (fn) { fn(ar, cb) })