Last change
on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | /*! run-parallel. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
---|
| 2 | module.exports = runParallel
|
---|
| 3 |
|
---|
| 4 | const queueMicrotask = require('queue-microtask')
|
---|
| 5 |
|
---|
| 6 | function runParallel (tasks, cb) {
|
---|
| 7 | let results, pending, keys
|
---|
| 8 | let isSync = true
|
---|
| 9 |
|
---|
| 10 | if (Array.isArray(tasks)) {
|
---|
| 11 | results = []
|
---|
| 12 | pending = tasks.length
|
---|
| 13 | } else {
|
---|
| 14 | keys = Object.keys(tasks)
|
---|
| 15 | results = {}
|
---|
| 16 | pending = keys.length
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | function done (err) {
|
---|
| 20 | function end () {
|
---|
| 21 | if (cb) cb(err, results)
|
---|
| 22 | cb = null
|
---|
| 23 | }
|
---|
| 24 | if (isSync) queueMicrotask(end)
|
---|
| 25 | else end()
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | function each (i, err, result) {
|
---|
| 29 | results[i] = result
|
---|
| 30 | if (--pending === 0 || err) {
|
---|
| 31 | done(err)
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | if (!pending) {
|
---|
| 36 | // empty
|
---|
| 37 | done(null)
|
---|
| 38 | } else if (keys) {
|
---|
| 39 | // object
|
---|
| 40 | keys.forEach(function (key) {
|
---|
| 41 | tasks[key](function (err, result) { each(key, err, result) })
|
---|
| 42 | })
|
---|
| 43 | } else {
|
---|
| 44 | // array
|
---|
| 45 | tasks.forEach(function (task, i) {
|
---|
| 46 | task(function (err, result) { each(i, err, result) })
|
---|
| 47 | })
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | isSync = false
|
---|
| 51 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.