UNPKG

1.06 kBJavaScriptView Raw
1import { wrapWithAbort } from './operators/withabort';
2import { throwIfAborted } from '../aborterror';
3/**
4 * Returns the value of the first element in the provided async-iterable that satisfies the provided testing function.
5 *
6 * @export
7 * @template T The type of the elements in the source sequence.
8 * @param {AsyncIterable<T>} source An async-iterable sequence whose elements to apply the predicate to.
9 * @param {FindOptions<T>} options The options for a predicate for filtering, thisArg for binding and AbortSignal for cancellation.
10 * @returns {(Promise<S | undefined>)} A promise with the value of the first element that matches the predicate.
11 */
12export async function find(source, options) {
13 const { ['signal']: signal, ['thisArg']: thisArg, ['predicate']: predicate } = options;
14 throwIfAborted(signal);
15 let i = 0;
16 for await (const item of wrapWithAbort(source, signal)) {
17 if (await predicate.call(thisArg, item, i++, signal)) {
18 return item;
19 }
20 }
21 return undefined;
22}
23
24//# sourceMappingURL=find.mjs.map