Created
November 1, 2018 22:35
-
-
Save rpgeeganage/f244d72c1d7587fddec18bc5626dfdab to your computer and use it in GitHub Desktop.
Some method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** returns boolean */ | |
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 some<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; | |
} | |
// You can use as follows | |
const array = [1, 2, 3, 4]; | |
const output = await aSome<number>(array, async (i) => { | |
return Promise.resolve(i > 2); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment