async-iterator-iteration.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. // https://github.com/tc39/proposal-array-from-async
  4. var global = require('../internals/global');
  5. var call = require('../internals/function-call');
  6. var aCallable = require('../internals/a-callable');
  7. var anObject = require('../internals/an-object');
  8. var getBuiltIn = require('../internals/get-built-in');
  9. var getMethod = require('../internals/get-method');
  10. var MAX_SAFE_INTEGER = 0x1FFFFFFFFFFFFF;
  11. var TypeError = global.TypeError;
  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 (iterator, fn, target) {
  18. anObject(iterator);
  19. var Promise = getBuiltIn('Promise');
  20. var next = aCallable(iterator.next);
  21. var index = 0;
  22. var MAPPING = fn !== undefined;
  23. if (MAPPING || !IS_TO_ARRAY) aCallable(fn);
  24. return new Promise(function (resolve, reject) {
  25. var closeIteration = function (method, argument) {
  26. try {
  27. var returnMethod = getMethod(iterator, 'return');
  28. if (returnMethod) {
  29. return Promise.resolve(call(returnMethod, iterator)).then(function () {
  30. method(argument);
  31. }, function (error) {
  32. reject(error);
  33. });
  34. }
  35. } catch (error2) {
  36. return reject(error2);
  37. } method(argument);
  38. };
  39. var onError = function (error) {
  40. closeIteration(reject, error);
  41. };
  42. var loop = function () {
  43. try {
  44. if (IS_TO_ARRAY && (index > MAX_SAFE_INTEGER) && MAPPING) {
  45. throw TypeError('The allowed number of iterations has been exceeded');
  46. }
  47. Promise.resolve(anObject(call(next, iterator))).then(function (step) {
  48. try {
  49. if (anObject(step).done) {
  50. if (IS_TO_ARRAY) {
  51. target.length = index;
  52. resolve(target);
  53. } else resolve(IS_SOME ? false : IS_EVERY || undefined);
  54. } else {
  55. var value = step.value;
  56. if (MAPPING) {
  57. Promise.resolve(IS_TO_ARRAY ? fn(value, index) : fn(value)).then(function (result) {
  58. if (IS_FOR_EACH) {
  59. loop();
  60. } else if (IS_EVERY) {
  61. result ? loop() : closeIteration(resolve, false);
  62. } else if (IS_TO_ARRAY) {
  63. target[index++] = result;
  64. loop();
  65. } else {
  66. result ? closeIteration(resolve, IS_SOME || value) : loop();
  67. }
  68. }, onError);
  69. } else {
  70. target[index++] = value;
  71. loop();
  72. }
  73. }
  74. } catch (error) { onError(error); }
  75. }, onError);
  76. } catch (error2) { onError(error2); }
  77. };
  78. loop();
  79. });
  80. };
  81. };
  82. module.exports = {
  83. toArray: createMethod(0),
  84. forEach: createMethod(1),
  85. every: createMethod(2),
  86. some: createMethod(3),
  87. find: createMethod(4)
  88. };