1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = reflectAll;
|
7 |
|
8 | var _reflect = require('./reflect.js');
|
9 |
|
10 | var _reflect2 = _interopRequireDefault(_reflect);
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 | /**
|
15 | * A helper function that wraps an array or an object of functions with `reflect`.
|
16 | *
|
17 | * @name reflectAll
|
18 | * @static
|
19 | * @memberOf module:Utils
|
20 | * @method
|
21 | * @see [async.reflect]{@link module:Utils.reflect}
|
22 | * @category Util
|
23 | * @param {Array|Object|Iterable} tasks - The collection of
|
24 | * [async functions]{@link AsyncFunction} to wrap in `async.reflect`.
|
25 | * @returns {Array} Returns an array of async functions, each wrapped in
|
26 | * `async.reflect`
|
27 | * @example
|
28 | *
|
29 | * let tasks = [
|
30 | * function(callback) {
|
31 | * setTimeout(function() {
|
32 | * callback(null, 'one');
|
33 | * }, 200);
|
34 | * },
|
35 | * function(callback) {
|
36 | * // do some more stuff but error ...
|
37 | * callback(new Error('bad stuff happened'));
|
38 | * },
|
39 | * function(callback) {
|
40 | * setTimeout(function() {
|
41 | * callback(null, 'two');
|
42 | * }, 100);
|
43 | * }
|
44 | * ];
|
45 | *
|
46 | * async.parallel(async.reflectAll(tasks),
|
47 | * // optional callback
|
48 | * function(err, results) {
|
49 | * // values
|
50 | * // results[0].value = 'one'
|
51 | * // results[1].error = Error('bad stuff happened')
|
52 | * // results[2].value = 'two'
|
53 | * });
|
54 | *
|
55 | * // an example using an object instead of an array
|
56 | * let tasks = {
|
57 | * one: function(callback) {
|
58 | * setTimeout(function() {
|
59 | * callback(null, 'one');
|
60 | * }, 200);
|
61 | * },
|
62 | * two: function(callback) {
|
63 | * callback('two');
|
64 | * },
|
65 | * three: function(callback) {
|
66 | * setTimeout(function() {
|
67 | * callback(null, 'three');
|
68 | * }, 100);
|
69 | * }
|
70 | * };
|
71 | *
|
72 | * async.parallel(async.reflectAll(tasks),
|
73 | * // optional callback
|
74 | * function(err, results) {
|
75 | * // values
|
76 | * // results.one.value = 'one'
|
77 | * // results.two.error = 'two'
|
78 | * // results.three.value = 'three'
|
79 | * });
|
80 | */
|
81 | function reflectAll(tasks) {
|
82 | var results;
|
83 | if (Array.isArray(tasks)) {
|
84 | results = tasks.map(_reflect2.default);
|
85 | } else {
|
86 | results = {};
|
87 | Object.keys(tasks).forEach(key => {
|
88 | results[key] = _reflect2.default.call(this, tasks[key]);
|
89 | });
|
90 | }
|
91 | return results;
|
92 | }
|
93 | module.exports = exports.default; |
\ | No newline at end of file |