Nenhuma descrição

index.js 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*! run-parallel. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  2. module.exports = runParallel
  3. const queueMicrotask = require('queue-microtask')
  4. function runParallel (tasks, cb) {
  5. let results, pending, keys
  6. let isSync = true
  7. if (Array.isArray(tasks)) {
  8. results = []
  9. pending = tasks.length
  10. } else {
  11. keys = Object.keys(tasks)
  12. results = {}
  13. pending = keys.length
  14. }
  15. function done (err) {
  16. function end () {
  17. if (cb) cb(err, results)
  18. cb = null
  19. }
  20. if (isSync) queueMicrotask(end)
  21. else end()
  22. }
  23. function each (i, err, result) {
  24. results[i] = result
  25. if (--pending === 0 || err) {
  26. done(err)
  27. }
  28. }
  29. if (!pending) {
  30. // empty
  31. done(null)
  32. } else if (keys) {
  33. // object
  34. keys.forEach(function (key) {
  35. tasks[key](function (err, result) { each(key, err, result) })
  36. })
  37. } else {
  38. // array
  39. tasks.forEach(function (task, i) {
  40. task(function (err, result) { each(i, err, result) })
  41. })
  42. }
  43. isSync = false
  44. }