esnext.async-iterator.as-indexed-pairs.js 919 B

12345678910111213141516171819202122232425262728
  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 createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  7. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {
  8. var state = this;
  9. var iterator = state.iterator;
  10. return Promise.resolve(anObject(apply(state.next, iterator, args))).then(function (step) {
  11. if (anObject(step).done) {
  12. state.done = true;
  13. return { done: true, value: undefined };
  14. }
  15. return { done: false, value: [state.index++, step.value] };
  16. });
  17. });
  18. $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
  19. asIndexedPairs: function asIndexedPairs() {
  20. return new AsyncIteratorProxy({
  21. iterator: anObject(this),
  22. index: 0
  23. });
  24. }
  25. });