12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- 'use strict';
- var GetIntrinsic = require('get-intrinsic');
- var $SyntaxError = require('es-errors/syntax');
- var $TypeError = require('es-errors/type');
- var $Promise = GetIntrinsic('%Promise%', true);
- var Call = require('./Call');
- var CompletionRecord = require('./CompletionRecord');
- var GetMethod = require('./GetMethod');
- var Type = require('./Type');
- var isIteratorRecord = require('../helpers/records/iterator-record');
- var callBound = require('call-bind/callBound');
- var $then = callBound('Promise.prototype.then', true);
- module.exports = function AsyncIteratorClose(iteratorRecord, completion) {
- if (!isIteratorRecord(iteratorRecord)) {
- throw new $TypeError('Assertion failed: `iteratorRecord` must be an Iterator Record');
- }
- 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;
- }
- );
- };
|