1
|
'use strict';
|
2
|
|
3
|
Object.defineProperty(exports, "__esModule", {
|
4
|
value: true
|
5
|
});
|
6
|
exports.default = parallelLimit;
|
7
|
|
8
|
var _eachOf = require('./eachOf');
|
9
|
|
10
|
var _eachOf2 = _interopRequireDefault(_eachOf);
|
11
|
|
12
|
var _parallel = require('./internal/parallel');
|
13
|
|
14
|
var _parallel2 = _interopRequireDefault(_parallel);
|
15
|
|
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17
|
|
18
|
/**
|
19
|
* Run the `tasks` collection of functions in parallel, without waiting until
|
20
|
* the previous function has completed. If any of the functions pass an error to
|
21
|
* its callback, the main `callback` is immediately called with the value of the
|
22
|
* error. Once the `tasks` have completed, the results are passed to the final
|
23
|
* `callback` as an array.
|
24
|
*
|
25
|
* **Note:** `parallel` is about kicking-off I/O tasks in parallel, not about
|
26
|
* parallel execution of code. If your tasks do not use any timers or perform
|
27
|
* any I/O, they will actually be executed in series. Any synchronous setup
|
28
|
* sections for each task will happen one after the other. JavaScript remains
|
29
|
* single-threaded.
|
30
|
*
|
31
|
* **Hint:** Use [`reflect`]{@link module:Utils.reflect} to continue the
|
32
|
* execution of other tasks when a task fails.
|
33
|
*
|
34
|
* It is also possible to use an object instead of an array. Each property will
|
35
|
* be run as a function and the results will be passed to the final `callback`
|
36
|
* as an object instead of an array. This can be a more readable way of handling
|
37
|
* results from {@link async.parallel}.
|
38
|
*
|
39
|
* @name parallel
|
40
|
* @static
|
41
|
* @memberOf module:ControlFlow
|
42
|
* @method
|
43
|
* @category Control Flow
|
44
|
* @param {Array|Iterable|Object} tasks - A collection of
|
45
|
* [async functions]{@link AsyncFunction} to run.
|
46
|
* Each async function can complete with any number of optional `result` values.
|
47
|
* @param {Function} [callback] - An optional callback to run once all the
|
48
|
* functions have completed successfully. This function gets a results array
|
49
|
* (or object) containing all the result arguments passed to the task callbacks.
|
50
|
* Invoked with (err, results).
|
51
|
*
|
52
|
* @example
|
53
|
* async.parallel([
|
54
|
* function(callback) {
|
55
|
* setTimeout(function() {
|
56
|
* callback(null, 'one');
|
57
|
* }, 200);
|
58
|
* },
|
59
|
* function(callback) {
|
60
|
* setTimeout(function() {
|
61
|
* callback(null, 'two');
|
62
|
* }, 100);
|
63
|
* }
|
64
|
* ],
|
65
|
* // optional callback
|
66
|
* function(err, results) {
|
67
|
* // the results array will equal ['one','two'] even though
|
68
|
* // the second function had a shorter timeout.
|
69
|
* });
|
70
|
*
|
71
|
* // an example using an object instead of an array
|
72
|
* async.parallel({
|
73
|
* one: function(callback) {
|
74
|
* setTimeout(function() {
|
75
|
* callback(null, 1);
|
76
|
* }, 200);
|
77
|
* },
|
78
|
* two: function(callback) {
|
79
|
* setTimeout(function() {
|
80
|
* callback(null, 2);
|
81
|
* }, 100);
|
82
|
* }
|
83
|
* }, function(err, results) {
|
84
|
* // results is now equals to: {one: 1, two: 2}
|
85
|
* });
|
86
|
*/
|
87
|
function parallelLimit(tasks, callback) {
|
88
|
(0, _parallel2.default)(_eachOf2.default, tasks, callback);
|
89
|
}
|
90
|
module.exports = exports['default'];
|