Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 899B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module.exports = runParallel
  2. function runParallel (tasks, cb) {
  3. var results, pending, keys
  4. var isSync = true
  5. if (Array.isArray(tasks)) {
  6. results = []
  7. pending = tasks.length
  8. } else {
  9. keys = Object.keys(tasks)
  10. results = {}
  11. pending = keys.length
  12. }
  13. function done (err) {
  14. function end () {
  15. if (cb) cb(err, results)
  16. cb = null
  17. }
  18. if (isSync) process.nextTick(end)
  19. else end()
  20. }
  21. function each (i, err, result) {
  22. results[i] = result
  23. if (--pending === 0 || err) {
  24. done(err)
  25. }
  26. }
  27. if (!pending) {
  28. // empty
  29. done(null)
  30. } else if (keys) {
  31. // object
  32. keys.forEach(function (key) {
  33. tasks[key](function (err, result) { each(key, err, result) })
  34. })
  35. } else {
  36. // array
  37. tasks.forEach(function (task, i) {
  38. task(function (err, result) { each(i, err, result) })
  39. })
  40. }
  41. isSync = false
  42. }