Skip to content

Commit

Permalink
mapAsyncIterator: simplify mapResult (#3013)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Apr 2, 2021
1 parent 6c53a27 commit 382dfe2
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/subscription/mapAsyncIterator.js
Expand Up @@ -24,10 +24,16 @@ export function mapAsyncIterator<T, U>(
};
}

function mapResult(result: IteratorResult<T, void>) {
return result.done
? result
: asyncMapValue(result.value, callback).then(iteratorResult, abruptClose);
async function mapResult(result: IteratorResult<T, void>) {
if (result.done) {
return result;
}

try {
return { value: await callback(result.value), done: false };
} catch (callbackError) {
return abruptClose(callbackError);
}
}

function mapReject(error: mixed) {
Expand Down Expand Up @@ -60,14 +66,3 @@ export function mapAsyncIterator<T, U>(
},
}: $FlowFixMe);
}

function asyncMapValue<T, U>(
value: T,
callback: (T) => PromiseOrValue<U>,
): Promise<U> {
return new Promise((resolve) => resolve(callback(value)));
}

function iteratorResult<T>(value: T): IteratorResult<T, void> {
return { value, done: false };
}

0 comments on commit 382dfe2

Please sign in to comment.