CreateAsyncFromSyncIterator.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 Call = require('./Call');
  7. var CreateIterResultObject = require('./CreateIterResultObject');
  8. var Get = require('./Get');
  9. var GetMethod = require('./GetMethod');
  10. var IteratorComplete = require('./IteratorComplete');
  11. var IteratorNext = require('./IteratorNext');
  12. var IteratorValue = require('./IteratorValue');
  13. var ObjectCreate = require('./ObjectCreate');
  14. var PromiseResolve = require('./PromiseResolve');
  15. var Type = require('./Type');
  16. var isIteratorRecord = require('../helpers/records/iterator-record');
  17. var SLOT = require('internal-slot');
  18. var callBound = require('call-bind/callBound');
  19. var $then = callBound('Promise.prototype.then', true);
  20. var AsyncFromSyncIteratorContinuation = function AsyncFromSyncIteratorContinuation(result) {
  21. if (Type(result) !== 'Object') {
  22. throw new $TypeError('Assertion failed: Type(O) is not Object');
  23. }
  24. if (arguments.length > 1) {
  25. throw new $TypeError('although AsyncFromSyncIteratorContinuation should take a second argument, it is not used in this implementation');
  26. }
  27. if (!$Promise) {
  28. throw new $SyntaxError('This environment does not support Promises.');
  29. }
  30. return new $Promise(function (resolve) {
  31. var done = IteratorComplete(result); // step 2
  32. var value = IteratorValue(result); // step 4
  33. var valueWrapper = PromiseResolve($Promise, value); // step 6
  34. // eslint-disable-next-line no-shadow
  35. var onFulfilled = function (value) { // steps 8-9
  36. return CreateIterResultObject(value, done); // step 8.a
  37. };
  38. resolve($then(valueWrapper, onFulfilled)); // step 11
  39. }); // step 12
  40. };
  41. var $AsyncFromSyncIteratorPrototype = GetIntrinsic('%AsyncFromSyncIteratorPrototype%', true) || {
  42. next: function next(value) {
  43. if (!$Promise) {
  44. throw new $SyntaxError('This environment does not support Promises.');
  45. }
  46. var O = this; // step 1
  47. SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
  48. var argsLength = arguments.length;
  49. return new $Promise(function (resolve) { // step 3
  50. var syncIteratorRecord = SLOT.get(O, '[[SyncIteratorRecord]]'); // step 4
  51. var result;
  52. if (argsLength > 0) {
  53. result = IteratorNext(syncIteratorRecord['[[Iterator]]'], value); // step 5.a
  54. } else { // step 6
  55. result = IteratorNext(syncIteratorRecord['[[Iterator]]']);// step 6.a
  56. }
  57. resolve(AsyncFromSyncIteratorContinuation(result)); // step 8
  58. });
  59. },
  60. 'return': function () {
  61. if (!$Promise) {
  62. throw new $SyntaxError('This environment does not support Promises.');
  63. }
  64. var O = this; // step 1
  65. SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
  66. var valueIsPresent = arguments.length > 0;
  67. var value = valueIsPresent ? arguments[0] : void undefined;
  68. return new $Promise(function (resolve, reject) { // step 3
  69. var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]']; // step 4
  70. var iteratorReturn = GetMethod(syncIterator, 'return'); // step 5
  71. if (typeof iteratorReturn === 'undefined') { // step 7
  72. var iterResult = CreateIterResultObject(value, true); // step 7.a
  73. Call(resolve, undefined, [iterResult]); // step 7.b
  74. return;
  75. }
  76. var result;
  77. if (valueIsPresent) { // step 8
  78. result = Call(iteratorReturn, syncIterator, [value]); // step 8.a
  79. } else { // step 9
  80. result = Call(iteratorReturn, syncIterator); // step 9.a
  81. }
  82. if (Type(result) !== 'Object') { // step 11
  83. Call(reject, undefined, [new $TypeError('Iterator `return` method returned a non-object value.')]); // step 11.a
  84. return;
  85. }
  86. resolve(AsyncFromSyncIteratorContinuation(result)); // step 12
  87. });
  88. },
  89. 'throw': function () {
  90. if (!$Promise) {
  91. throw new $SyntaxError('This environment does not support Promises.');
  92. }
  93. var O = this; // step 1
  94. SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
  95. var valueIsPresent = arguments.length > 0;
  96. var value = valueIsPresent ? arguments[0] : void undefined;
  97. return new $Promise(function (resolve, reject) { // step 3
  98. var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]']; // step 4
  99. var throwMethod = GetMethod(syncIterator, 'throw'); // step 5
  100. if (typeof throwMethod === 'undefined') { // step 7
  101. Call(reject, undefined, [value]); // step 7.a
  102. return;
  103. }
  104. var result;
  105. if (valueIsPresent) { // step 8
  106. result = Call(throwMethod, syncIterator, [value]); // step 8.a
  107. } else { // step 9
  108. result = Call(throwMethod, syncIterator); // step 9.a
  109. }
  110. if (Type(result) !== 'Object') { // step 11
  111. Call(reject, undefined, [new $TypeError('Iterator `throw` method returned a non-object value.')]); // step 11.a
  112. return;
  113. }
  114. resolve(AsyncFromSyncIteratorContinuation(result/* , promiseCapability */)); // step 12
  115. });
  116. }
  117. };
  118. // https://262.ecma-international.org/9.0/#sec-createasyncfromsynciterator
  119. module.exports = function CreateAsyncFromSyncIterator(syncIteratorRecord) {
  120. if (!isIteratorRecord(syncIteratorRecord)) {
  121. throw new $TypeError('Assertion failed: `syncIteratorRecord` is not an Iterator Record');
  122. }
  123. // var asyncIterator = ObjectCreate(%AsyncFromSyncIteratorPrototype%, « [[SyncIteratorRecord]] »); // step 1
  124. var asyncIterator = ObjectCreate($AsyncFromSyncIteratorPrototype);
  125. SLOT.set(asyncIterator, '[[SyncIteratorRecord]]', syncIteratorRecord); // step 2
  126. var nextMethod = Get(asyncIterator, 'next'); // step 3
  127. return { // steps 3-4
  128. '[[Iterator]]': asyncIterator,
  129. '[[NextMethod]]': nextMethod,
  130. '[[Done]]': false
  131. };
  132. };