Sha256: e918d1940bbf9225d7cec0ba962f54937f2050be78162137b5c04708d538b667

Contents?: true

Size: 1.44 KB

Versions: 70

Compression:

Stored size: 1.44 KB

Contents

/*
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 () {
  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
    , l = list.length
    , a = l * n
  if (!a) return cb_(null, [])
  function cb (er) {
    if (er && !errState) errState = er

    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 (--a === 0) cb_.apply(null, [errState].concat(data))
  }
  // 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) })
  })
}

Version data entries

70 entries across 69 versions & 4 rubygems

Version Path
locomotivecms-4.0.0.alpha1 app/javascript/node_modules/slide/lib/async-map.js
locomotivecms-3.4.0 app/javascript/node_modules/slide/lib/async-map.js
dragonfly_puppeteer-0.1.0 node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.6.3 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.6.2 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/slide/lib/async-map.js
node-compiler-0.9.0 vendor/node-v7.2.1/deps/npm/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.6.1 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
node-compiler-0.8.0 vendor/node-v7.2.0/deps/npm/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.6 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
node-compiler-0.7.0 vendor/node-v6.9.1/deps/npm/node_modules/slide/lib/async-map.js
node-compiler-0.7.0 vendor/node-v7.1.0/deps/npm/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.5.19 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.5.18 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.5.17 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.5.16 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.5.15 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.5.14 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.5.13 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js
trusty-festivity-extension-2.5.12 node_modules/bower/node_modules/insight/node_modules/configstore/node_modules/write-file-atomic/node_modules/slide/lib/async-map.js