esnext.async-iterator.map.js 963 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. var $ = require('../internals/export');
  4. var aFunction = require('../internals/a-function');
  5. var anObject = require('../internals/an-object');
  6. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  7. var AsyncIteratorProxy = createAsyncIteratorProxy(function (arg, Promise) {
  8. var state = this;
  9. var mapper = state.mapper;
  10. return Promise.resolve(anObject(state.next.call(state.iterator, arg))).then(function (step) {
  11. if (anObject(step).done) {
  12. state.done = true;
  13. return { done: true, value: undefined };
  14. }
  15. return Promise.resolve(mapper(step.value)).then(function (value) {
  16. return { done: false, value: value };
  17. });
  18. });
  19. });
  20. $({ target: 'AsyncIterator', proto: true, real: true }, {
  21. map: function map(mapper) {
  22. return new AsyncIteratorProxy({
  23. iterator: anObject(this),
  24. mapper: aFunction(mapper)
  25. });
  26. }
  27. });