Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapAsyncIterator: simplify abruptClose #3014

Merged
merged 1 commit into from Apr 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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