| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = series;
|
|---|
| 7 |
|
|---|
| 8 | var _parallel2 = require('./internal/parallel.js');
|
|---|
| 9 |
|
|---|
| 10 | var _parallel3 = _interopRequireDefault(_parallel2);
|
|---|
| 11 |
|
|---|
| 12 | var _eachOfSeries = require('./eachOfSeries.js');
|
|---|
| 13 |
|
|---|
| 14 | var _eachOfSeries2 = _interopRequireDefault(_eachOfSeries);
|
|---|
| 15 |
|
|---|
| 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Run the functions in the `tasks` collection in series, each one running once
|
|---|
| 20 | * the previous function has completed. If any functions in the series pass an
|
|---|
| 21 | * error to its callback, no more functions are run, and `callback` is
|
|---|
| 22 | * immediately called with the value of the error. Otherwise, `callback`
|
|---|
| 23 | * receives an array of results when `tasks` have completed.
|
|---|
| 24 | *
|
|---|
| 25 | * It is also possible to use an object instead of an array. Each property will
|
|---|
| 26 | * be run as a function, and the results will be passed to the final `callback`
|
|---|
| 27 | * as an object instead of an array. This can be a more readable way of handling
|
|---|
| 28 | * results from {@link async.series}.
|
|---|
| 29 | *
|
|---|
| 30 | * **Note** that while many implementations preserve the order of object
|
|---|
| 31 | * properties, the [ECMAScript Language Specification](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6)
|
|---|
| 32 | * explicitly states that
|
|---|
| 33 | *
|
|---|
| 34 | * > The mechanics and order of enumerating the properties is not specified.
|
|---|
| 35 | *
|
|---|
| 36 | * So if you rely on the order in which your series of functions are executed,
|
|---|
| 37 | * and want this to work on all platforms, consider using an array.
|
|---|
| 38 | *
|
|---|
| 39 | * @name series
|
|---|
| 40 | * @static
|
|---|
| 41 | * @memberOf module:ControlFlow
|
|---|
| 42 | * @method
|
|---|
| 43 | * @category Control Flow
|
|---|
| 44 | * @param {Array|Iterable|AsyncIterable|Object} tasks - A collection containing
|
|---|
| 45 | * [async functions]{@link AsyncFunction} to run in series.
|
|---|
| 46 | * Each 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. This function gets a results array (or object)
|
|---|
| 49 | * containing all the result arguments passed to the `task` callbacks. Invoked
|
|---|
| 50 | * with (err, result).
|
|---|
| 51 | * @return {Promise} a promise, if no callback is passed
|
|---|
| 52 | * @example
|
|---|
| 53 | *
|
|---|
| 54 | * //Using Callbacks
|
|---|
| 55 | * async.series([
|
|---|
| 56 | * function(callback) {
|
|---|
| 57 | * setTimeout(function() {
|
|---|
| 58 | * // do some async task
|
|---|
| 59 | * callback(null, 'one');
|
|---|
| 60 | * }, 200);
|
|---|
| 61 | * },
|
|---|
| 62 | * function(callback) {
|
|---|
| 63 | * setTimeout(function() {
|
|---|
| 64 | * // then do another async task
|
|---|
| 65 | * callback(null, 'two');
|
|---|
| 66 | * }, 100);
|
|---|
| 67 | * }
|
|---|
| 68 | * ], function(err, results) {
|
|---|
| 69 | * console.log(results);
|
|---|
| 70 | * // results is equal to ['one','two']
|
|---|
| 71 | * });
|
|---|
| 72 | *
|
|---|
| 73 | * // an example using objects instead of arrays
|
|---|
| 74 | * async.series({
|
|---|
| 75 | * one: function(callback) {
|
|---|
| 76 | * setTimeout(function() {
|
|---|
| 77 | * // do some async task
|
|---|
| 78 | * callback(null, 1);
|
|---|
| 79 | * }, 200);
|
|---|
| 80 | * },
|
|---|
| 81 | * two: function(callback) {
|
|---|
| 82 | * setTimeout(function() {
|
|---|
| 83 | * // then do another async task
|
|---|
| 84 | * callback(null, 2);
|
|---|
| 85 | * }, 100);
|
|---|
| 86 | * }
|
|---|
| 87 | * }, function(err, results) {
|
|---|
| 88 | * console.log(results);
|
|---|
| 89 | * // results is equal to: { one: 1, two: 2 }
|
|---|
| 90 | * });
|
|---|
| 91 | *
|
|---|
| 92 | * //Using Promises
|
|---|
| 93 | * async.series([
|
|---|
| 94 | * function(callback) {
|
|---|
| 95 | * setTimeout(function() {
|
|---|
| 96 | * callback(null, 'one');
|
|---|
| 97 | * }, 200);
|
|---|
| 98 | * },
|
|---|
| 99 | * function(callback) {
|
|---|
| 100 | * setTimeout(function() {
|
|---|
| 101 | * callback(null, 'two');
|
|---|
| 102 | * }, 100);
|
|---|
| 103 | * }
|
|---|
| 104 | * ]).then(results => {
|
|---|
| 105 | * console.log(results);
|
|---|
| 106 | * // results is equal to ['one','two']
|
|---|
| 107 | * }).catch(err => {
|
|---|
| 108 | * console.log(err);
|
|---|
| 109 | * });
|
|---|
| 110 | *
|
|---|
| 111 | * // an example using an object instead of an array
|
|---|
| 112 | * async.series({
|
|---|
| 113 | * one: function(callback) {
|
|---|
| 114 | * setTimeout(function() {
|
|---|
| 115 | * // do some async task
|
|---|
| 116 | * callback(null, 1);
|
|---|
| 117 | * }, 200);
|
|---|
| 118 | * },
|
|---|
| 119 | * two: function(callback) {
|
|---|
| 120 | * setTimeout(function() {
|
|---|
| 121 | * // then do another async task
|
|---|
| 122 | * callback(null, 2);
|
|---|
| 123 | * }, 100);
|
|---|
| 124 | * }
|
|---|
| 125 | * }).then(results => {
|
|---|
| 126 | * console.log(results);
|
|---|
| 127 | * // results is equal to: { one: 1, two: 2 }
|
|---|
| 128 | * }).catch(err => {
|
|---|
| 129 | * console.log(err);
|
|---|
| 130 | * });
|
|---|
| 131 | *
|
|---|
| 132 | * //Using async/await
|
|---|
| 133 | * async () => {
|
|---|
| 134 | * try {
|
|---|
| 135 | * let results = await async.series([
|
|---|
| 136 | * function(callback) {
|
|---|
| 137 | * setTimeout(function() {
|
|---|
| 138 | * // do some async task
|
|---|
| 139 | * callback(null, 'one');
|
|---|
| 140 | * }, 200);
|
|---|
| 141 | * },
|
|---|
| 142 | * function(callback) {
|
|---|
| 143 | * setTimeout(function() {
|
|---|
| 144 | * // then do another async task
|
|---|
| 145 | * callback(null, 'two');
|
|---|
| 146 | * }, 100);
|
|---|
| 147 | * }
|
|---|
| 148 | * ]);
|
|---|
| 149 | * console.log(results);
|
|---|
| 150 | * // results is equal to ['one','two']
|
|---|
| 151 | * }
|
|---|
| 152 | * catch (err) {
|
|---|
| 153 | * console.log(err);
|
|---|
| 154 | * }
|
|---|
| 155 | * }
|
|---|
| 156 | *
|
|---|
| 157 | * // an example using an object instead of an array
|
|---|
| 158 | * async () => {
|
|---|
| 159 | * try {
|
|---|
| 160 | * let results = await async.parallel({
|
|---|
| 161 | * one: function(callback) {
|
|---|
| 162 | * setTimeout(function() {
|
|---|
| 163 | * // do some async task
|
|---|
| 164 | * callback(null, 1);
|
|---|
| 165 | * }, 200);
|
|---|
| 166 | * },
|
|---|
| 167 | * two: function(callback) {
|
|---|
| 168 | * setTimeout(function() {
|
|---|
| 169 | * // then do another async task
|
|---|
| 170 | * callback(null, 2);
|
|---|
| 171 | * }, 100);
|
|---|
| 172 | * }
|
|---|
| 173 | * });
|
|---|
| 174 | * console.log(results);
|
|---|
| 175 | * // results is equal to: { one: 1, two: 2 }
|
|---|
| 176 | * }
|
|---|
| 177 | * catch (err) {
|
|---|
| 178 | * console.log(err);
|
|---|
| 179 | * }
|
|---|
| 180 | * }
|
|---|
| 181 | *
|
|---|
| 182 | */
|
|---|
| 183 | function series(tasks, callback) {
|
|---|
| 184 | return (0, _parallel3.default)(_eachOfSeries2.default, tasks, callback);
|
|---|
| 185 | }
|
|---|
| 186 | module.exports = exports.default; |
|---|