esnext.async-iterator.take.js 1017 B

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