Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/async-array-utils. A collection of array-related async utilities.
array array-methods async promise utils
Something went wrong. Try again.
1.1 kB · 46 lines
TypeScript
at vitest-5
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import asyncForEachStrict from './forEach_strict.js';
function asyncFindIndex<T>( arr: T[], fn: (cur: T, idx: number, arr: T[]) => Promise<false>,): Promise<-1>;function asyncFindIndex<T>( arr: T[], fn: (cur: T, idx: number, arr: T[]) => Promise<boolean>,): Promise<number>;function asyncFindIndex<T>( arr: T[], fn: (cur: T, idx: number, arr: T[]) => Promise<boolean>,): Promise<number> { let resolved: boolean; return new Promise<number>((resolve, reject) => { asyncForEachStrict( arr, (cur, idx, arr2) => new Promise<void>((resolve2, reject2) => { if (resolved) { return; } fn(cur, idx, arr2) .then((result) => { if (result) { resolve(idx); resolved = true; } resolve2(); }) .catch(reject2); }), ) .then(() => { resolve(-1); resolved = true; }) .catch((error) => { reject(error); resolved = true; }); });}
export default asyncFindIndex;