async-iterator-iteration.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. var record = getIteratorDirect(object);
  19. var Promise = getBuiltIn('Promise');
  20. var iterator = record.iterator;
  21. var next = record.next;
  22. var counter = 0;
  23. var MAPPING = fn !== undefined;
  24. if (MAPPING || !IS_TO_ARRAY) aCallable(fn);
  25. return new Promise(function (resolve, reject) {
  26. var ifAbruptCloseAsyncIterator = function (error) {
  27. closeAsyncIteration(iterator, reject, error, reject);
  28. };
  29. var loop = function () {
  30. try {
  31. if (MAPPING) try {
  32. doesNotExceedSafeInteger(counter);
  33. } catch (error5) { ifAbruptCloseAsyncIterator(error5); }
  34. Promise.resolve(anObject(call(next, iterator))).then(function (step) {
  35. try {
  36. if (anObject(step).done) {
  37. if (IS_TO_ARRAY) {
  38. target.length = counter;
  39. resolve(target);
  40. } else resolve(IS_SOME ? false : IS_EVERY || undefined);
  41. } else {
  42. var value = step.value;
  43. try {
  44. if (MAPPING) {
  45. var result = fn(value, counter);
  46. var handler = function ($result) {
  47. if (IS_FOR_EACH) {
  48. loop();
  49. } else if (IS_EVERY) {
  50. $result ? loop() : closeAsyncIteration(iterator, resolve, false, reject);
  51. } else if (IS_TO_ARRAY) {
  52. try {
  53. target[counter++] = $result;
  54. loop();
  55. } catch (error4) { ifAbruptCloseAsyncIterator(error4); }
  56. } else {
  57. $result ? closeAsyncIteration(iterator, resolve, IS_SOME || value, reject) : loop();
  58. }
  59. };
  60. if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
  61. else handler(result);
  62. } else {
  63. target[counter++] = value;
  64. loop();
  65. }
  66. } catch (error3) { ifAbruptCloseAsyncIterator(error3); }
  67. }
  68. } catch (error2) { reject(error2); }
  69. }, reject);
  70. } catch (error) { reject(error); }
  71. };
  72. loop();
  73. });
  74. };
  75. };
  76. module.exports = {
  77. toArray: createMethod(0),
  78. forEach: createMethod(1),
  79. every: createMethod(2),
  80. some: createMethod(3),
  81. find: createMethod(4)
  82. };