async-iterator-iteration.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. // https://github.com/tc39/proposal-array-from-async
  4. var call = require('../internals/function-call');
  5. var aCallable = require('../internals/a-callable');
  6. var anObject = require('../internals/an-object');
  7. var isObject = require('../internals/is-object');
  8. var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
  9. var getBuiltIn = require('../internals/get-built-in');
  10. var getIteratorDirect = require('../internals/get-iterator-direct');
  11. var closeAsyncIteration = require('../internals/async-iterator-close');
  12. var createMethod = function (TYPE) {
  13. var IS_TO_ARRAY = TYPE === 0;
  14. var IS_FOR_EACH = TYPE === 1;
  15. var IS_EVERY = TYPE === 2;
  16. var IS_SOME = TYPE === 3;
  17. return function (object, fn, target) {
  18. anObject(object);
  19. var MAPPING = fn !== undefined;
  20. if (MAPPING || !IS_TO_ARRAY) aCallable(fn);
  21. var record = getIteratorDirect(object);
  22. var Promise = getBuiltIn('Promise');
  23. var iterator = record.iterator;
  24. var next = record.next;
  25. var counter = 0;
  26. return new Promise(function (resolve, reject) {
  27. var ifAbruptCloseAsyncIterator = function (error) {
  28. closeAsyncIteration(iterator, reject, error, reject);
  29. };
  30. var loop = function () {
  31. try {
  32. if (MAPPING) try {
  33. doesNotExceedSafeInteger(counter);
  34. } catch (error5) { ifAbruptCloseAsyncIterator(error5); }
  35. Promise.resolve(anObject(call(next, iterator))).then(function (step) {
  36. try {
  37. if (anObject(step).done) {
  38. if (IS_TO_ARRAY) {
  39. target.length = counter;
  40. resolve(target);
  41. } else resolve(IS_SOME ? false : IS_EVERY || undefined);
  42. } else {
  43. var value = step.value;
  44. try {
  45. if (MAPPING) {
  46. var result = fn(value, counter);
  47. var handler = function ($result) {
  48. if (IS_FOR_EACH) {
  49. loop();
  50. } else if (IS_EVERY) {
  51. $result ? loop() : closeAsyncIteration(iterator, resolve, false, reject);
  52. } else if (IS_TO_ARRAY) {
  53. try {
  54. target[counter++] = $result;
  55. loop();
  56. } catch (error4) { ifAbruptCloseAsyncIterator(error4); }
  57. } else {
  58. $result ? closeAsyncIteration(iterator, resolve, IS_SOME || value, reject) : loop();
  59. }
  60. };
  61. if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
  62. else handler(result);
  63. } else {
  64. target[counter++] = value;
  65. loop();
  66. }
  67. } catch (error3) { ifAbruptCloseAsyncIterator(error3); }
  68. }
  69. } catch (error2) { reject(error2); }
  70. }, reject);
  71. } catch (error) { reject(error); }
  72. };
  73. loop();
  74. });
  75. };
  76. };
  77. module.exports = {
  78. toArray: createMethod(0),
  79. forEach: createMethod(1),
  80. every: createMethod(2),
  81. some: createMethod(3),
  82. find: createMethod(4)
  83. };