esnext.async-iterator.map.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  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 aCallable = require('../internals/a-callable');
  6. var anObject = require('../internals/an-object');
  7. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  8. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {
  9. var state = this;
  10. var mapper = state.mapper;
  11. return Promise.resolve(anObject(apply(state.next, state.iterator, args))).then(function (step) {
  12. if (anObject(step).done) {
  13. state.done = true;
  14. return { done: true, value: undefined };
  15. }
  16. return Promise.resolve(mapper(step.value)).then(function (value) {
  17. return { done: false, value: value };
  18. });
  19. });
  20. });
  21. $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
  22. map: function map(mapper) {
  23. return new AsyncIteratorProxy({
  24. iterator: anObject(this),
  25. mapper: aCallable(mapper)
  26. });
  27. }
  28. });