async-iterator-create-proxy.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. var call = require('../internals/function-call');
  3. var aCallable = require('../internals/a-callable');
  4. var anObject = require('../internals/an-object');
  5. var create = require('../internals/object-create');
  6. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  7. var redefineAll = require('../internals/redefine-all');
  8. var wellKnownSymbol = require('../internals/well-known-symbol');
  9. var InternalStateModule = require('../internals/internal-state');
  10. var getBuiltIn = require('../internals/get-built-in');
  11. var getMethod = require('../internals/get-method');
  12. var AsyncIteratorPrototype = require('../internals/async-iterator-prototype');
  13. var Promise = getBuiltIn('Promise');
  14. var ASYNC_ITERATOR_PROXY = 'AsyncIteratorProxy';
  15. var setInternalState = InternalStateModule.set;
  16. var getInternalState = InternalStateModule.getterFor(ASYNC_ITERATOR_PROXY);
  17. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  18. module.exports = function (nextHandler, IS_ITERATOR) {
  19. var AsyncIteratorProxy = function AsyncIterator(state) {
  20. state.type = ASYNC_ITERATOR_PROXY;
  21. state.next = aCallable(state.iterator.next);
  22. state.done = false;
  23. state.ignoreArgument = !IS_ITERATOR;
  24. setInternalState(this, state);
  25. };
  26. AsyncIteratorProxy.prototype = redefineAll(create(AsyncIteratorPrototype), {
  27. next: function next(arg) {
  28. var that = this;
  29. var hasArgument = !!arguments.length;
  30. return new Promise(function (resolve) {
  31. var state = getInternalState(that);
  32. var args = hasArgument ? [state.ignoreArgument ? undefined : arg] : IS_ITERATOR ? [] : [undefined];
  33. state.ignoreArgument = false;
  34. resolve(state.done ? { done: true, value: undefined } : anObject(call(nextHandler, state, Promise, args)));
  35. });
  36. },
  37. 'return': function (value) {
  38. var that = this;
  39. return new Promise(function (resolve, reject) {
  40. var state = getInternalState(that);
  41. var iterator = state.iterator;
  42. state.done = true;
  43. var $$return = getMethod(iterator, 'return');
  44. if ($$return === undefined) return resolve({ done: true, value: value });
  45. Promise.resolve(call($$return, iterator, value)).then(function (result) {
  46. anObject(result);
  47. resolve({ done: true, value: value });
  48. }, reject);
  49. });
  50. },
  51. 'throw': function (value) {
  52. var that = this;
  53. return new Promise(function (resolve, reject) {
  54. var state = getInternalState(that);
  55. var iterator = state.iterator;
  56. state.done = true;
  57. var $$throw = getMethod(iterator, 'throw');
  58. if ($$throw === undefined) return reject(value);
  59. resolve(call($$throw, iterator, value));
  60. });
  61. }
  62. });
  63. if (!IS_ITERATOR) {
  64. createNonEnumerableProperty(AsyncIteratorProxy.prototype, TO_STRING_TAG, 'Generator');
  65. }
  66. return AsyncIteratorProxy;
  67. };