esnext.async-iterator.drop.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var anObject = require('../internals/an-object');
  5. var getIteratorDirect = require('../internals/get-iterator-direct');
  6. var notANaN = require('../internals/not-a-nan');
  7. var toPositiveInteger = require('../internals/to-positive-integer');
  8. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  9. var createIterResultObject = require('../internals/create-iter-result-object');
  10. var IS_PURE = require('../internals/is-pure');
  11. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
  12. var state = this;
  13. return new Promise(function (resolve, reject) {
  14. var doneAndReject = function (error) {
  15. state.done = true;
  16. reject(error);
  17. };
  18. var loop = function () {
  19. try {
  20. Promise.resolve(anObject(call(state.next, state.iterator))).then(function (step) {
  21. try {
  22. if (anObject(step).done) {
  23. state.done = true;
  24. resolve(createIterResultObject(undefined, true));
  25. } else if (state.remaining) {
  26. state.remaining--;
  27. loop();
  28. } else resolve(createIterResultObject(step.value, false));
  29. } catch (err) { doneAndReject(err); }
  30. }, doneAndReject);
  31. } catch (error) { doneAndReject(error); }
  32. };
  33. loop();
  34. });
  35. });
  36. // `AsyncIterator.prototype.drop` method
  37. // https://github.com/tc39/proposal-async-iterator-helpers
  38. $({ target: 'AsyncIterator', proto: true, real: true, forced: IS_PURE }, {
  39. drop: function drop(limit) {
  40. anObject(this);
  41. var remaining = toPositiveInteger(notANaN(+limit));
  42. return new AsyncIteratorProxy(getIteratorDirect(this), {
  43. remaining: remaining
  44. });
  45. }
  46. });