rpgeeganage/async-ray

View on GitHub
lib/methods/a_some.ts

Summary

Maintainability
A
0 mins
Test Coverage
/** returns boolean */
export type CallBackSome<T> = (
  value: T,
  index?: number,
  collection?: T[]
) => Promise<boolean>;

/**
 * Async Some function
 *
 * @export
 * @template T
 * @param {T[]} elements
 * @param {CallBackSome<T>} cb
 * @returns {Promise<boolean>}
 */
export async function aSome<T>(
  elements: T[],
  cb: CallBackSome<T>
): Promise<boolean> {
  for (const [index, element] of elements.entries()) {
    if (await cb(element, index, elements)) {
      return true;
    }
  }

  return false;
}