[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 4 |
|
---|
| 5 | var $SyntaxError = require('es-errors/syntax');
|
---|
| 6 | var $TypeError = require('es-errors/type');
|
---|
| 7 | var $Promise = GetIntrinsic('%Promise%', true);
|
---|
| 8 |
|
---|
| 9 | var Call = require('./Call');
|
---|
| 10 | var CompletionRecord = require('./CompletionRecord');
|
---|
| 11 | var GetMethod = require('./GetMethod');
|
---|
| 12 | var Type = require('./Type');
|
---|
| 13 |
|
---|
| 14 | var isIteratorRecord = require('../helpers/records/iterator-record');
|
---|
| 15 |
|
---|
| 16 | var callBound = require('call-bind/callBound');
|
---|
| 17 |
|
---|
| 18 | var $then = callBound('Promise.prototype.then', true);
|
---|
| 19 |
|
---|
| 20 | // https://262.ecma-international.org/12.0/#sec-asynciteratorclose
|
---|
| 21 |
|
---|
| 22 | module.exports = function AsyncIteratorClose(iteratorRecord, completion) {
|
---|
| 23 | if (!isIteratorRecord(iteratorRecord)) {
|
---|
| 24 | throw new $TypeError('Assertion failed: `iteratorRecord` must be an Iterator Record'); // step 1
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | if (!(completion instanceof CompletionRecord)) {
|
---|
| 28 | throw new $TypeError('Assertion failed: completion is not a Completion Record instance'); // step 2
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | if (!$then) {
|
---|
| 32 | throw new $SyntaxError('This environment does not support Promises.');
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | var iterator = iteratorRecord['[[Iterator]]']; // step 3
|
---|
| 36 |
|
---|
| 37 | return $then(
|
---|
| 38 | $then(
|
---|
| 39 | $then(
|
---|
| 40 | new $Promise(function (resolve) {
|
---|
| 41 | resolve(GetMethod(iterator, 'return')); // step 4
|
---|
| 42 | // resolve(Call(ret, iterator, [])); // step 6
|
---|
| 43 | }),
|
---|
| 44 | function (returnV) { // step 5.a
|
---|
| 45 | if (typeof returnV === 'undefined') {
|
---|
| 46 | return completion; // step 5.b
|
---|
| 47 | }
|
---|
| 48 | return Call(returnV, iterator); // step 5.c, 5.d.
|
---|
| 49 | }
|
---|
| 50 | ),
|
---|
| 51 | null,
|
---|
| 52 | function (e) {
|
---|
| 53 | if (completion.type() === 'throw') {
|
---|
| 54 | completion['?'](); // step 6
|
---|
| 55 | } else {
|
---|
| 56 | throw e; // step 7
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | ),
|
---|
| 60 | function (innerResult) { // step 8
|
---|
| 61 | if (completion.type() === 'throw') {
|
---|
| 62 | completion['?'](); // step 6
|
---|
| 63 | }
|
---|
| 64 | if (Type(innerResult) !== 'Object') {
|
---|
| 65 | throw new $TypeError('`innerResult` must be an Object'); // step 10
|
---|
| 66 | }
|
---|
| 67 | return completion;
|
---|
| 68 | }
|
---|
| 69 | );
|
---|
| 70 | };
|
---|