esnext.async-iterator.drop.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 state = this;
  9. return new Promise(function (resolve, reject) {
  10. var loop = function () {
  11. try {
  12. Promise.resolve(
  13. anObject(state.next.call(state.iterator, state.remaining ? undefined : arg))
  14. ).then(function (step) {
  15. try {
  16. if (anObject(step).done) {
  17. state.done = true;
  18. resolve({ done: true, value: undefined });
  19. } else if (state.remaining) {
  20. state.remaining--;
  21. loop();
  22. } else resolve({ done: false, value: step.value });
  23. } catch (err) { reject(err); }
  24. }, reject);
  25. } catch (error) { reject(error); }
  26. };
  27. loop();
  28. });
  29. });
  30. $({ target: 'AsyncIterator', proto: true, real: true }, {
  31. drop: function drop(limit) {
  32. return new AsyncIteratorProxy({
  33. iterator: anObject(this),
  34. remaining: toPositiveInteger(limit)
  35. });
  36. }
  37. });