2 throw new Error("TODO: Not yet implemented.")
7 Like asyncMap, but only can take a single cb, and guarantees
8 the order of the results.
11 module.exports = asyncMapOrdered
13 function asyncMapOrdered (list, fn, cb_) {
14 if (typeof cb_ !== "function") throw new Error(
15 "No callback provided to asyncMapOrdered")
17 if (typeof fn !== "function") throw new Error(
18 "No map function provided to asyncMapOrdered")
20 if (list === undefined || list === null) return cb_(null, [])
21 if (!Array.isArray(list)) list = [list]
22 if (!list.length) return cb_(null, [])
31 function cb (index) { return function () {
34 var argLen = arguments.length
35 maxArgLen = Math.max(maxArgLen, argLen)
36 res[index] = argLen === 1 ? [er] : Array.apply(null, arguments)
38 // see if any new things have been added.
39 if (list.length > l) {
40 var newList = list.slice(l)
41 a += (list.length - l)
44 process.nextTick(function () {
45 newList.forEach(function (ar, i) { fn(ar, cb(i + oldLen)) })
49 if (er || --a === 0) {
51 cb_.apply(null, [errState].concat(flip(res, resCount, maxArgLen)))
54 // expect the supplied cb function to be called
55 // "n" times for each thing in the array.
56 list.forEach(function (ar) {
57 steps.forEach(function (fn, i) { fn(ar, cb(i)) })
61 function flip (res, resCount, argLen) {
63 // res = [[er, x, y], [er, x1, y1], [er, x2, y2, z2]]
64 // return [[x, x1, x2], [y, y1, y2], [undefined, undefined, z2]]