IteratorNext.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var isObject = require('es-object-atoms/isObject');
  4. var Call = require('./Call');
  5. var isIteratorRecord = require('../helpers/records/iterator-record');
  6. // https://262.ecma-international.org/16.0/#sec-iteratornext
  7. module.exports = function IteratorNext(iteratorRecord) {
  8. if (!isIteratorRecord(iteratorRecord)) {
  9. throw new $TypeError('Assertion failed: `iteratorRecord` must be an Iterator Record'); // step 1
  10. }
  11. var result;
  12. try {
  13. if (arguments.length < 2) { // step 1
  14. result = Call(iteratorRecord['[[NextMethod]]'], iteratorRecord['[[Iterator]]']); // step 1.a
  15. } else { // step 2
  16. result = Call(iteratorRecord['[[NextMethod]]'], iteratorRecord['[[Iterator]]'], [arguments[1]]); // step 2.a
  17. }
  18. } catch (e) { // step 3
  19. // eslint-disable-next-line no-param-reassign
  20. iteratorRecord['[[Done]]'] = true; // step 3.a
  21. throw e; // step 3.b
  22. }
  23. if (!isObject(result)) { // step 5
  24. // eslint-disable-next-line no-param-reassign
  25. iteratorRecord['[[Done]]'] = true; // step 5.a
  26. throw new $TypeError('iterator next must return an object'); // step 5.b
  27. }
  28. return result; // step 6
  29. };