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

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