UNPKG

991 BJavaScriptView Raw
1import { wrapWithAbort } from './operators/withabort';
2import { throwIfAborted } from '../aborterror';
3/**
4 * Returns the first element of an async-iterable sequence that matches the predicate if provided, or undefined if no such element exists.
5 *
6 * @export
7 * @template T The type of the elements in the source sequence.
8 * @param {AsyncIterable<T>} source Source async-enumerable sequence.
9 * @returns {(Promise<S | undefined>)} A Promise containing the first element in the async-iterable sequence,
10 * or a default value if no such element exists.
11 */
12export async function first(source, options) {
13 const { ['signal']: signal, ['thisArg']: thisArg, ['predicate']: predicate = async () => true } = 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=first.mjs.map