esnext.async-iterator.from.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // https://github.com/tc39/proposal-iterator-helpers
  2. var $ = require('../internals/export');
  3. var apply = require('../internals/function-apply');
  4. var anObject = require('../internals/an-object');
  5. var toObject = require('../internals/to-object');
  6. var isPrototypeOf = require('../internals/object-is-prototype-of');
  7. var AsyncIteratorPrototype = require('../internals/async-iterator-prototype');
  8. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  9. var getAsyncIterator = require('../internals/get-async-iterator');
  10. var getIterator = require('../internals/get-iterator');
  11. var getIteratorMethod = require('../internals/get-iterator-method');
  12. var getMethod = require('../internals/get-method');
  13. var wellKnownSymbol = require('../internals/well-known-symbol');
  14. var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator');
  15. var ASYNC_ITERATOR = wellKnownSymbol('asyncIterator');
  16. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {
  17. return anObject(apply(this.next, this.iterator, args));
  18. }, true);
  19. $({ target: 'AsyncIterator', stat: true, forced: true }, {
  20. from: function from(O) {
  21. var object = toObject(O);
  22. var usingIterator = getMethod(object, ASYNC_ITERATOR);
  23. var iterator;
  24. if (usingIterator) {
  25. iterator = getAsyncIterator(object, usingIterator);
  26. if (isPrototypeOf(AsyncIteratorPrototype, iterator)) return iterator;
  27. }
  28. if (iterator === undefined) {
  29. usingIterator = getIteratorMethod(object);
  30. if (usingIterator) return new AsyncFromSyncIterator(getIterator(object, usingIterator));
  31. }
  32. return new AsyncIteratorProxy({ iterator: iterator !== undefined ? iterator : object });
  33. }
  34. });