source: imaps-frontend/node_modules/es-abstract/2021/AsyncIteratorClose.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.9 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $SyntaxError = require('es-errors/syntax');
6var $TypeError = require('es-errors/type');
7var $Promise = GetIntrinsic('%Promise%', true);
8
9var Call = require('./Call');
10var CompletionRecord = require('./CompletionRecord');
11var GetMethod = require('./GetMethod');
12var Type = require('./Type');
13
14var isIteratorRecord = require('../helpers/records/iterator-record');
15
16var callBound = require('call-bind/callBound');
17
18var $then = callBound('Promise.prototype.then', true);
19
20// https://262.ecma-international.org/12.0/#sec-asynciteratorclose
21
22module.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};
Note: See TracBrowser for help on using the repository browser.