AsyncFromSyncIteratorPrototype.js [plain text]
function next(value)
{
"use strict";
const promiseCapability = @newPromiseCapability(@Promise);
if (!@isObject(this) || !@isObject(@getByIdDirectPrivate(this, "syncIterator"))) {
promiseCapability.@reject.@call(@undefined, @makeTypeError('Iterator is not an object.'));
return promiseCapability.@promise;
}
const syncIterator = @getByIdDirectPrivate(this, "syncIterator");
try {
const { value: nextValue, done: nextDone } = @getByIdDirectPrivate(this, "nextMethod").@call(syncIterator, value);
const valueWrapperCapability = @newPromiseCapability(@Promise);
valueWrapperCapability.@resolve.@call(@undefined, nextValue);
valueWrapperCapability.@promise.@then(
function (result) { promiseCapability.@resolve.@call(@undefined, { value: result, done: !!nextDone }); },
function (error) { promiseCapability.@reject.@call(@undefined, error); });
} catch(e) {
promiseCapability.@reject.@call(@undefined, e);
}
return promiseCapability.@promise;
}
function return(value)
{
"use strict";
const promiseCapability = @newPromiseCapability(@Promise);
if (!@isObject(this) || !@isObject(@getByIdDirectPrivate(this, "syncIterator"))) {
promiseCapability.@reject.@call(@undefined, @makeTypeError('Iterator is not an object.'));
return promiseCapability.@promise;
}
const syncIterator = @getByIdDirectPrivate(this, "syncIterator");
let returnMethod;
try {
returnMethod = syncIterator.return;
} catch (e) {
promiseCapability.@reject.@call(@undefined, e);
return promiseCapability.@promise;
}
if (returnMethod === @undefined) {
promiseCapability.@resolve.@call(@undefined, { value, done: true });
return promiseCapability.@promise;
}
try {
const returnResult = returnMethod.@call(syncIterator, value);
if (!@isObject(returnResult)) {
promiseCapability.@reject.@call(@undefined, @makeTypeError('Iterator result interface is not an object.'));
return promiseCapability.@promise;
}
const { value: resultValue, done: resultDone } = returnResult;
const valueWrapperCapability = @newPromiseCapability(@Promise);
valueWrapperCapability.@resolve.@call(@undefined, resultValue);
valueWrapperCapability.@promise.@then(
function (result) { promiseCapability.@resolve.@call(@undefined, { value: result, done: resultDone }); },
function (error) { promiseCapability.@reject.@call(@undefined, error); });
} catch (e) {
promiseCapability.@reject.@call(@undefined, e);
}
return promiseCapability.@promise;
}
function throw(exception)
{
"use strict";
const promiseCapability = @newPromiseCapability(@Promise);
if (!@isObject(this) || !@isObject(@getByIdDirectPrivate(this, "syncIterator"))) {
promiseCapability.@reject.@call(@undefined, @makeTypeError('Iterator is not an object.'));
return promiseCapability.@promise;
}
const syncIterator = @getByIdDirectPrivate(this, "syncIterator");
let throwMethod;
try {
throwMethod = syncIterator.throw;
} catch (e) {
promiseCapability.@reject.@call(@undefined, e);
return promiseCapability.@promise;
}
if (throwMethod === @undefined) {
promiseCapability.@reject.@call(@undefined, exception);
return promiseCapability.@promise;
}
try {
const throwResult = throwMethod.@call(syncIterator, exception);
if (!@isObject(throwResult)) {
promiseCapability.@reject.@call(@undefined, @makeTypeError('Iterator result interface is not an object.'));
return promiseCapability.@promise;
}
const { value: throwValue, done: throwDone } = throwResult;
const valueWrapperCapability = @newPromiseCapability(@Promise);
valueWrapperCapability.@resolve.@call(@undefined, throwValue);
valueWrapperCapability.@promise.@then(
function (result) { promiseCapability.@resolve.@call(@undefined, { value: result, done: throwDone }); },
function (error) { promiseCapability.@reject.@call(@undefined, error); });
} catch (e) {
promiseCapability.@reject.@call(@undefined, e);
}
return promiseCapability.@promise;
}
@globalPrivate
function createAsyncFromSyncIterator(syncIterator, nextMethod)
{
"use strict";
if (!@isObject(syncIterator))
@throwTypeError('Only objects can be wrapped by async-from-sync wrapper');
return new @AsyncFromSyncIterator(syncIterator, nextMethod);
}
@globalPrivate
@constructor
function AsyncFromSyncIterator(syncIterator, nextMethod)
{
"use strict";
@putByIdDirectPrivate(this, "syncIterator", syncIterator);
@putByIdDirectPrivate(this, "nextMethod", nextMethod);
}