AsyncFromSyncIteratorContinuation.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $SyntaxError = require('es-errors/syntax');
  4. var $TypeError = require('es-errors/type');
  5. var $Promise = GetIntrinsic('%Promise%', true);
  6. var callBound = require('call-bind/callBound');
  7. var CreateIterResultObject = require('./CreateIterResultObject');
  8. var IteratorComplete = require('./IteratorComplete');
  9. var IteratorValue = require('./IteratorValue');
  10. var PromiseResolve = require('./PromiseResolve');
  11. var Type = require('./Type');
  12. var $then = callBound('Promise.prototype.then', true);
  13. // https://262.ecma-international.org/10.0/#sec-asyncfromsynciteratorcontinuation
  14. module.exports = function AsyncFromSyncIteratorContinuation(result) {
  15. if (Type(result) !== 'Object') {
  16. throw new $TypeError('Assertion failed: Type(O) is not Object');
  17. }
  18. if (arguments.length > 1) {
  19. throw new $SyntaxError('although AsyncFromSyncIteratorContinuation should take a second argument, it is not used in this implementation');
  20. }
  21. if (!$Promise) {
  22. throw new $SyntaxError('This environment does not support Promises.');
  23. }
  24. return new $Promise(function (resolve) {
  25. var done = IteratorComplete(result); // step 2
  26. var value = IteratorValue(result); // step 4
  27. var valueWrapper = PromiseResolve($Promise, value); // step 6
  28. // eslint-disable-next-line no-shadow
  29. var onFulfilled = function (value) { // steps 8-9
  30. return CreateIterResultObject(value, done); // step 8.a
  31. };
  32. resolve($then(valueWrapper, onFulfilled)); // step 11
  33. }); // step 12
  34. };