Skip to content

Commit

Permalink
Update $jscomp.makeAsyncIterator to correctly implement spec change…
Browse files Browse the repository at this point in the history
…s for when the wrapped sync iterator is missing a `throw` method.

Previously, the async iterator would also elide the `throw` method, but github.com/tc39/ecma262/pull/2600 specifies that it should instead provide a `throw` method that (1) closes the underlying sync iterator (via `return`), and (2) throws a new `TypeError` indicating that the wrapped sync iterator was non-conformant.

PiperOrigin-RevId: 653268505
  • Loading branch information
shicks authored and copybara-github committed Jul 17, 2024
1 parent b746df8 commit 7cd5e6a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/com/google/javascript/jscomp/js/es6/util/makeasynciterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,22 @@ $jscomp.AsyncIteratorFromSyncWrapper = function(iterator) {
return Promise.resolve(iterator.next(param));
};

if (iterator['throw'] !== undefined) {
/**
* @param {?} param
* @return {!Promise<!IIterableResult<T>>}
*/
this['throw'] = function(param) {
return Promise.resolve(iterator['throw'](param));
};
}
/**
* @param {?} param
* @return {!Promise<!IIterableResult<T>>}
*/
this['throw'] = function(param) {
return new Promise(function(resolve, reject) {
var rethrow = iterator['throw'];
if (rethrow !== undefined) {
resolve(rethrow.call(iterator, param));
} else {
var close = iterator['return'];
if (close !== undefined) close.call(iterator);
reject(new TypeError('no `throw` method'));
}
});
};

if (iterator['return'] !== undefined) {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/resources/resources.json

Large diffs are not rendered by default.

0 comments on commit 7cd5e6a

Please sign in to comment.