async-iterator-indexed.js 1004 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. var call = require('../internals/function-call');
  4. var anObject = require('../internals/an-object');
  5. var getIteratorDirect = require('../internals/get-iterator-direct');
  6. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  7. var createIterResultObject = require('../internals/create-iter-result-object');
  8. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
  9. var state = this;
  10. var iterator = state.iterator;
  11. return Promise.resolve(anObject(call(state.next, iterator))).then(function (step) {
  12. if (anObject(step).done) {
  13. state.done = true;
  14. return createIterResultObject(undefined, true);
  15. }
  16. return createIterResultObject([state.index++, step.value], false);
  17. }).then(null, function (error) {
  18. state.done = true;
  19. throw error;
  20. });
  21. });
  22. module.exports = function indexed() {
  23. return new AsyncIteratorProxy(getIteratorDirect(this), {
  24. index: 0
  25. });
  26. };