[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/9.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 new $Promise(function (resolve) {
|
---|
| 38 | var ret = GetMethod(iterator, 'return'); // step 4
|
---|
| 39 |
|
---|
| 40 | if (typeof ret === 'undefined') {
|
---|
| 41 | resolve(completion); // step 5
|
---|
| 42 | } else {
|
---|
| 43 | resolve($then(
|
---|
| 44 | new $Promise(function (resolve2) {
|
---|
| 45 | // process.exit(42);
|
---|
| 46 | resolve2(Call(ret, iterator, [])); // step 6
|
---|
| 47 | }),
|
---|
| 48 | function (innerResult) {
|
---|
| 49 | if (Type(innerResult) !== 'Object') {
|
---|
| 50 | throw new $TypeError('`innerResult` must be an Object'); // step 10
|
---|
| 51 | }
|
---|
| 52 | return completion;
|
---|
| 53 | },
|
---|
| 54 | function (e) {
|
---|
| 55 | if (completion.type() === 'throw') {
|
---|
| 56 | completion['?'](); // step 8
|
---|
| 57 | } else {
|
---|
| 58 | throw e; // step 9
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | ));
|
---|
| 62 | }
|
---|
| 63 | });
|
---|
| 64 | };
|
---|