UNPKG

462 BJavaScriptView Raw
1export async function single(source, predicate = () => true) {
2 let result;
3 let hasResult = false;
4 let i = 0;
5 for await (let item of source) {
6 if (hasResult && (await predicate(item, i++))) {
7 throw new Error('More than one element was found');
8 }
9 if (await predicate(item, i++)) {
10 result = item;
11 hasResult = true;
12 }
13 }
14 return result;
15}
16
17//# sourceMappingURL=single.mjs.map