Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/subscription/mapAsyncIterator.js
Original file line number Diff line number Diff line change
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