chain.js 660 B

1234567891011121314151617181920
  1. module.exports = chain
  2. var bindActor = require("./bind-actor.js")
  3. chain.first = {} ; chain.last = {}
  4. function chain (things, cb) {
  5. var res = []
  6. ;(function LOOP (i, len) {
  7. if (i >= len) return cb(null,res)
  8. if (Array.isArray(things[i]))
  9. things[i] = bindActor.apply(null,
  10. things[i].map(function(i){
  11. return (i===chain.first) ? res[0]
  12. : (i===chain.last)
  13. ? res[res.length - 1] : i }))
  14. if (!things[i]) return LOOP(i + 1, len)
  15. things[i](function (er, data) {
  16. if (er) return cb(er, res)
  17. if (data !== undefined) res = res.concat(data)
  18. LOOP(i + 1, len)
  19. })
  20. })(0, things.length) }