Skip to content

Commit

Permalink
mapAsyncIterator: simplify abruptClose (#3014)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Apr 2, 2021
1 parent 382dfe2 commit 665fc9c
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/subscription/mapAsyncIterator.js
Expand Up @@ -14,14 +14,16 @@ export function mapAsyncIterator<T, U>(
// $FlowFixMe[prop-missing]
const iteratorMethod = iterable[Symbol.asyncIterator];
const iterator: any = iteratorMethod.call(iterable);
let $return: any;
let abruptClose;
if (typeof iterator.return === 'function') {
$return = iterator.return;
abruptClose = (error: mixed) => {
const rethrow = () => Promise.reject(error);
return $return.call(iterator).then(rethrow, rethrow);
};

async function abruptClose(error: mixed) {
if (typeof iterator.return === 'function') {
try {
await iterator.return();
} catch (_e) {
/* ignore error */
}
}
throw error;
}

async function mapResult(result: IteratorResult<T, void>) {
Expand Down Expand Up @@ -51,8 +53,8 @@ export function mapAsyncIterator<T, U>(
return iterator.next().then(mapResult, mapReject);
},
return() {
return $return
? $return.call(iterator).then(mapResult, mapReject)
return typeof iterator.return === 'function'
? iterator.return().then(mapResult, mapReject)
: Promise.resolve({ value: undefined, done: true });
},
throw(error?: mixed): Promise<IteratorResult<U, void>> {
Expand Down

0 comments on commit 665fc9c

Please sign in to comment.