IteratorStepValue.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var Get = require('./Get');
  4. var IteratorComplete = require('./IteratorComplete');
  5. var IteratorNext = require('./IteratorNext');
  6. var isIteratorRecord = require('../helpers/records/iterator-record');
  7. // https://262.ecma-international.org/15.0/#sec-iteratorstepvalue
  8. module.exports = function IteratorStepValue(iteratorRecord) {
  9. if (!isIteratorRecord(iteratorRecord)) {
  10. throw new $TypeError('Assertion failed: `iteratorRecord` must be an Iterator Record');
  11. }
  12. /* eslint no-param-reassign: 0 */
  13. var result;
  14. try {
  15. result = IteratorNext(iteratorRecord); // step 1
  16. } catch (e) { // step 2
  17. iteratorRecord['[[Done]]'] = true; // step 2.a
  18. throw e; // step 2.b
  19. }
  20. var done;
  21. try {
  22. done = IteratorComplete(result); // step 4
  23. } catch (e) { // step 5
  24. iteratorRecord['[[Done]]'] = true; // step 5.a
  25. throw e; // step 5.b
  26. }
  27. if (done) { // step 7
  28. iteratorRecord['[[Done]]'] = true; // step 7.a
  29. return 'DONE'; // step 7.b
  30. }
  31. var value;
  32. try {
  33. value = Get(result, 'value'); // step 8
  34. } catch (e) { // step 9
  35. iteratorRecord['[[Done]]'] = true; // step 9.a
  36. throw e; // step 10
  37. }
  38. return value; // step 10
  39. };