UNPKG

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