esnext.async-iterator.flat-map.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var aCallable = require('../internals/a-callable');
  5. var anObject = require('../internals/an-object');
  6. var isObject = require('../internals/is-object');
  7. var getIteratorDirect = require('../internals/get-iterator-direct');
  8. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  9. var createIterResultObject = require('../internals/create-iter-result-object');
  10. var getAsyncIteratorFlattenable = require('../internals/get-async-iterator-flattenable');
  11. var closeAsyncIteration = require('../internals/async-iterator-close');
  12. var IS_PURE = require('../internals/is-pure');
  13. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
  14. var state = this;
  15. var iterator = state.iterator;
  16. var mapper = state.mapper;
  17. return new Promise(function (resolve, reject) {
  18. var doneAndReject = function (error) {
  19. state.done = true;
  20. reject(error);
  21. };
  22. var ifAbruptCloseAsyncIterator = function (error) {
  23. closeAsyncIteration(iterator, doneAndReject, error, doneAndReject);
  24. };
  25. var outerLoop = function () {
  26. try {
  27. Promise.resolve(anObject(call(state.next, iterator))).then(function (step) {
  28. try {
  29. if (anObject(step).done) {
  30. state.done = true;
  31. resolve(createIterResultObject(undefined, true));
  32. } else {
  33. var value = step.value;
  34. try {
  35. var result = mapper(value, state.counter++);
  36. var handler = function (mapped) {
  37. try {
  38. state.inner = getAsyncIteratorFlattenable(mapped);
  39. innerLoop();
  40. } catch (error4) { ifAbruptCloseAsyncIterator(error4); }
  41. };
  42. if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
  43. else handler(result);
  44. } catch (error3) { ifAbruptCloseAsyncIterator(error3); }
  45. }
  46. } catch (error2) { doneAndReject(error2); }
  47. }, doneAndReject);
  48. } catch (error) { doneAndReject(error); }
  49. };
  50. var innerLoop = function () {
  51. var inner = state.inner;
  52. if (inner) {
  53. try {
  54. Promise.resolve(anObject(call(inner.next, inner.iterator))).then(function (result) {
  55. try {
  56. if (anObject(result).done) {
  57. state.inner = null;
  58. outerLoop();
  59. } else resolve(createIterResultObject(result.value, false));
  60. } catch (error1) { ifAbruptCloseAsyncIterator(error1); }
  61. }, ifAbruptCloseAsyncIterator);
  62. } catch (error) { ifAbruptCloseAsyncIterator(error); }
  63. } else outerLoop();
  64. };
  65. innerLoop();
  66. });
  67. });
  68. // `AsyncIterator.prototype.flaMap` method
  69. // https://github.com/tc39/proposal-async-iterator-helpers
  70. $({ target: 'AsyncIterator', proto: true, real: true, forced: IS_PURE }, {
  71. flatMap: function flatMap(mapper) {
  72. anObject(this);
  73. aCallable(mapper);
  74. return new AsyncIteratorProxy(getIteratorDirect(this), {
  75. mapper: mapper,
  76. inner: null
  77. });
  78. }
  79. });