series.js 5.7 KB

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