esnext.async-iterator.take.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. var $ = require('../internals/export');
  4. var apply = require('../internals/function-apply');
  5. var call = require('../internals/function-call');
  6. var anObject = require('../internals/an-object');
  7. var toPositiveInteger = require('../internals/to-positive-integer');
  8. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  9. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {
  10. var iterator = this.iterator;
  11. var returnMethod, result;
  12. if (!this.remaining--) {
  13. result = { done: true, value: undefined };
  14. this.done = true;
  15. returnMethod = iterator['return'];
  16. if (returnMethod !== undefined) {
  17. return Promise.resolve(call(returnMethod, iterator)).then(function () {
  18. return result;
  19. });
  20. }
  21. return result;
  22. } return apply(this.next, iterator, args);
  23. });
  24. $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
  25. take: function take(limit) {
  26. return new AsyncIteratorProxy({
  27. iterator: anObject(this),
  28. remaining: toPositiveInteger(limit)
  29. });
  30. }
  31. });