Skip to content

Commit 665fc9c

Browse files
mapAsyncIterator: simplify abruptClose (#3014)
1 parent 382dfe2 commit 665fc9c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/subscription/mapAsyncIterator.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ export function mapAsyncIterator<T, U>(
1414
// $FlowFixMe[prop-missing]
1515
const iteratorMethod = iterable[Symbol.asyncIterator];
1616
const iterator: any = iteratorMethod.call(iterable);
17-
let $return: any;
18-
let abruptClose;
19-
if (typeof iterator.return === 'function') {
20-
$return = iterator.return;
21-
abruptClose = (error: mixed) => {
22-
const rethrow = () => Promise.reject(error);
23-
return $return.call(iterator).then(rethrow, rethrow);
24-
};
17+
18+
async function abruptClose(error: mixed) {
19+
if (typeof iterator.return === 'function') {
20+
try {
21+
await iterator.return();
22+
} catch (_e) {
23+
/* ignore error */
24+
}
25+
}
26+
throw error;
2527
}
2628

2729
async function mapResult(result: IteratorResult<T, void>) {
@@ -51,8 +53,8 @@ export function mapAsyncIterator<T, U>(
5153
return iterator.next().then(mapResult, mapReject);
5254
},
5355
return() {
54-
return $return
55-
? $return.call(iterator).then(mapResult, mapReject)
56+
return typeof iterator.return === 'function'
57+
? iterator.return().then(mapResult, mapReject)
5658
: Promise.resolve({ value: undefined, done: true });
5759
},
5860
throw(error?: mixed): Promise<IteratorResult<U, void>> {

0 commit comments

Comments
 (0)