1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 'use strict';
- var GetIntrinsic = require('get-intrinsic');
- var $SyntaxError = GetIntrinsic('%SyntaxError%');
- var $TypeError = GetIntrinsic('%TypeError%');
- var $Promise = GetIntrinsic('%Promise%', true);
- var Call = require('./Call');
- var CompletionRecord = require('./CompletionRecord');
- var GetMethod = require('./GetMethod');
- var Type = require('./Type');
- var assertRecord = require('../helpers/assertRecord');
- var callBound = require('call-bind/callBound');
- var $then = callBound('Promise.prototype.then', true);
- module.exports = function AsyncIteratorClose(iteratorRecord, completion) {
- assertRecord(Type, 'Iterator Record', 'iteratorRecord', iteratorRecord);
- if (!(completion instanceof CompletionRecord)) {
- throw new $TypeError('Assertion failed: completion is not a Completion Record instance');
- }
- if (!$then) {
- throw new $SyntaxError('This environment does not support Promises.');
- }
- var iterator = iteratorRecord['[[Iterator]]'];
- return $then(
- $then(
- $then(
- new $Promise(function (resolve) {
- resolve(GetMethod(iterator, 'return'));
-
- }),
- function (returnV) {
- if (typeof returnV === 'undefined') {
- return completion;
- }
- return Call(returnV, iterator);
- }
- ),
- null,
- function (e) {
- if (completion.type() === 'throw') {
- completion['?']();
- } else {
- throw e;
- }
- }
- ),
- function (innerResult) {
- if (completion.type() === 'throw') {
- completion['?']();
- }
- if (Type(innerResult) !== 'Object') {
- throw new $TypeError('`innerResult` must be an Object');
- }
- return completion;
- }
- );
- };
|