source: imaps-frontend/node_modules/es-abstract/2018/CreateAsyncFromSyncIterator.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[d565449]1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $SyntaxError = require('es-errors/syntax');
6var $TypeError = require('es-errors/type');
7var $Promise = GetIntrinsic('%Promise%', true);
8
9var Call = require('./Call');
10var CreateIterResultObject = require('./CreateIterResultObject');
11var Get = require('./Get');
12var GetMethod = require('./GetMethod');
13var IteratorComplete = require('./IteratorComplete');
14var IteratorNext = require('./IteratorNext');
15var IteratorValue = require('./IteratorValue');
16var ObjectCreate = require('./ObjectCreate');
17var PromiseResolve = require('./PromiseResolve');
18var Type = require('./Type');
19
20var isIteratorRecord = require('../helpers/records/iterator-record');
21
22var SLOT = require('internal-slot');
23
24var callBound = require('call-bind/callBound');
25
26var $then = callBound('Promise.prototype.then', true);
27
28var AsyncFromSyncIteratorContinuation = function AsyncFromSyncIteratorContinuation(result) {
29 if (Type(result) !== 'Object') {
30 throw new $TypeError('Assertion failed: Type(O) is not Object');
31 }
32
33 if (arguments.length > 1) {
34 throw new $TypeError('although AsyncFromSyncIteratorContinuation should take a second argument, it is not used in this implementation');
35 }
36
37 if (!$Promise) {
38 throw new $SyntaxError('This environment does not support Promises.');
39 }
40
41 return new $Promise(function (resolve) {
42 var done = IteratorComplete(result); // step 2
43 var value = IteratorValue(result); // step 4
44 var valueWrapper = PromiseResolve($Promise, value); // step 6
45
46 // eslint-disable-next-line no-shadow
47 var onFulfilled = function (value) { // steps 8-9
48 return CreateIterResultObject(value, done); // step 8.a
49 };
50 resolve($then(valueWrapper, onFulfilled)); // step 11
51 }); // step 12
52};
53
54var $AsyncFromSyncIteratorPrototype = GetIntrinsic('%AsyncFromSyncIteratorPrototype%', true) || {
55 next: function next(value) {
56 if (!$Promise) {
57 throw new $SyntaxError('This environment does not support Promises.');
58 }
59
60 var O = this; // step 1
61
62 SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
63
64 var argsLength = arguments.length;
65
66 return new $Promise(function (resolve) { // step 3
67 var syncIteratorRecord = SLOT.get(O, '[[SyncIteratorRecord]]'); // step 4
68 var result;
69 if (argsLength > 0) {
70 result = IteratorNext(syncIteratorRecord['[[Iterator]]'], value); // step 5.a
71 } else { // step 6
72 result = IteratorNext(syncIteratorRecord['[[Iterator]]']);// step 6.a
73 }
74 resolve(AsyncFromSyncIteratorContinuation(result)); // step 8
75 });
76 },
77 'return': function () {
78 if (!$Promise) {
79 throw new $SyntaxError('This environment does not support Promises.');
80 }
81
82 var O = this; // step 1
83
84 SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
85
86 var valueIsPresent = arguments.length > 0;
87 var value = valueIsPresent ? arguments[0] : void undefined;
88
89 return new $Promise(function (resolve, reject) { // step 3
90 var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]']; // step 4
91 var iteratorReturn = GetMethod(syncIterator, 'return'); // step 5
92
93 if (typeof iteratorReturn === 'undefined') { // step 7
94 var iterResult = CreateIterResultObject(value, true); // step 7.a
95 Call(resolve, undefined, [iterResult]); // step 7.b
96 return;
97 }
98 var result;
99 if (valueIsPresent) { // step 8
100 result = Call(iteratorReturn, syncIterator, [value]); // step 8.a
101 } else { // step 9
102 result = Call(iteratorReturn, syncIterator); // step 9.a
103 }
104 if (Type(result) !== 'Object') { // step 11
105 Call(reject, undefined, [new $TypeError('Iterator `return` method returned a non-object value.')]); // step 11.a
106 return;
107 }
108
109 resolve(AsyncFromSyncIteratorContinuation(result)); // step 12
110 });
111 },
112 'throw': function () {
113 if (!$Promise) {
114 throw new $SyntaxError('This environment does not support Promises.');
115 }
116
117 var O = this; // step 1
118
119 SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
120
121 var valueIsPresent = arguments.length > 0;
122 var value = valueIsPresent ? arguments[0] : void undefined;
123
124 return new $Promise(function (resolve, reject) { // step 3
125 var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]']; // step 4
126
127 var throwMethod = GetMethod(syncIterator, 'throw'); // step 5
128
129 if (typeof throwMethod === 'undefined') { // step 7
130 Call(reject, undefined, [value]); // step 7.a
131 return;
132 }
133
134 var result;
135 if (valueIsPresent) { // step 8
136 result = Call(throwMethod, syncIterator, [value]); // step 8.a
137 } else { // step 9
138 result = Call(throwMethod, syncIterator); // step 9.a
139 }
140 if (Type(result) !== 'Object') { // step 11
141 Call(reject, undefined, [new $TypeError('Iterator `throw` method returned a non-object value.')]); // step 11.a
142 return;
143 }
144
145 resolve(AsyncFromSyncIteratorContinuation(result/* , promiseCapability */)); // step 12
146 });
147 }
148};
149
150// https://262.ecma-international.org/9.0/#sec-createasyncfromsynciterator
151
152module.exports = function CreateAsyncFromSyncIterator(syncIteratorRecord) {
153 if (!isIteratorRecord(syncIteratorRecord)) {
154 throw new $TypeError('Assertion failed: `syncIteratorRecord` is not an Iterator Record');
155 }
156
157 // var asyncIterator = ObjectCreate(%AsyncFromSyncIteratorPrototype%, « [[SyncIteratorRecord]] »); // step 1
158 var asyncIterator = ObjectCreate($AsyncFromSyncIteratorPrototype);
159
160 SLOT.set(asyncIterator, '[[SyncIteratorRecord]]', syncIteratorRecord); // step 2
161
162 var nextMethod = Get(asyncIterator, 'next'); // step 3
163
164 return { // steps 3-4
165 '[[Iterator]]': asyncIterator,
166 '[[NextMethod]]': nextMethod,
167 '[[Done]]': false
168 };
169};
Note: See TracBrowser for help on using the repository browser.