esnext.async-iterator.drop.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 anObject = require('../internals/an-object');
  6. var toPositiveInteger = require('../internals/to-positive-integer');
  7. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  8. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {
  9. var state = this;
  10. return new Promise(function (resolve, reject) {
  11. var loop = function () {
  12. try {
  13. Promise.resolve(
  14. anObject(apply(state.next, state.iterator, state.remaining ? [] : args))
  15. ).then(function (step) {
  16. try {
  17. if (anObject(step).done) {
  18. state.done = true;
  19. resolve({ done: true, value: undefined });
  20. } else if (state.remaining) {
  21. state.remaining--;
  22. loop();
  23. } else resolve({ done: false, value: step.value });
  24. } catch (err) { reject(err); }
  25. }, reject);
  26. } catch (error) { reject(error); }
  27. };
  28. loop();
  29. });
  30. });
  31. $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
  32. drop: function drop(limit) {
  33. return new AsyncIteratorProxy({
  34. iterator: anObject(this),
  35. remaining: toPositiveInteger(limit)
  36. });
  37. }
  38. });