eachSeries.js

  1. 'use strict';
  2. const { AigleProxy } = require('aigle-core');
  3. const Aigle = require('./aigle');
  4. const { INTERNAL, PENDING } = require('./internal/util');
  5. const { execute, setSeries } = require('./internal/collection');
  6. class EachSeries extends AigleProxy {
  7. constructor(collection, iterator, set = setDefault) {
  8. super();
  9. this._iterator = iterator;
  10. this._promise = new Aigle(INTERNAL);
  11. this._index = 0;
  12. this._coll = undefined;
  13. this._rest = undefined;
  14. this._size = undefined;
  15. this._keys = undefined;
  16. this._result = undefined;
  17. this._iterate = undefined;
  18. if (collection === PENDING) {
  19. this._set = set;
  20. this._iterate = this._callResolve;
  21. this._callResolve = execute;
  22. } else {
  23. set.call(this, collection);
  24. }
  25. }
  26. _execute() {
  27. if (this._rest === 0) {
  28. this._promise._resolve(this._result);
  29. } else {
  30. this._iterate();
  31. }
  32. return this._promise;
  33. }
  34. _callResolve(value) {
  35. if (--this._rest === 0 || value === false) {
  36. this._promise._resolve(this._result);
  37. } else {
  38. this._iterate();
  39. }
  40. }
  41. _callReject(reason) {
  42. this._promise._reject(reason);
  43. }
  44. }
  45. module.exports = { eachSeries, EachSeries };
  46. function setDefault(collection) {
  47. setSeries.call(this, collection);
  48. this._result = collection;
  49. return this;
  50. }
  51. /**
  52. * `Aigle.eachSeries` is almost the same as [`Aigle.each`](https://suguru03.github.io/aigle/docs/Aigle.html#each), but it will work in series.
  53. * @param {Array|Object} collection
  54. * @param {Function} iterator
  55. * @return {Aigle} Returns an Aigle instance
  56. * @example
  57. * const order = [];
  58. * const collection = [1, 4, 2];
  59. * const iterator = num => {
  60. * return Aigle.delay(num * 10)
  61. * .then(() => order.push(num));
  62. * };
  63. * Aigle.eachSeries(collection, iterator)
  64. * .then(value => {
  65. * console.log(value); // undefined
  66. * console.log(order); // [1, 4, 2];
  67. * });
  68. *
  69. * @example
  70. * const order = [];
  71. * const collection = { a: 1, b: 4, c: 2 };
  72. * const iterator = num => {
  73. * return Aigle.delay(num * 10)
  74. * .then(() => order.push(num));
  75. * };
  76. * Aigle.eachSeries(collection, iterator)
  77. * .then(value => {
  78. * console.log(value); // undefined
  79. * console.log(order); // [1, 4, 2];
  80. * });
  81. *
  82. * @example
  83. * const order = [];
  84. * const collection = [1, 4, 2];
  85. * const iterator = num => {
  86. * return Aigle.delay(num * 10)
  87. * .then(() => {
  88. * order.push(num);
  89. * return num !== 4; // break
  90. * });
  91. * };
  92. * Aigle.eachSeries(collection, iterator)
  93. * .then(value => {
  94. * console.log(value); // undefined
  95. * console.log(order); // [1, 4];
  96. * });
  97. */
  98. function eachSeries(collection, iterator) {
  99. return new EachSeries(collection, iterator)._execute();
  100. }