Skip to content

Instantly share code, notes, and snippets.

@rpgeeganage
Last active November 1, 2018 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpgeeganage/05ca784c7c0c1c061e5ed25e4c6eb84c to your computer and use it in GitHub Desktop.
Save rpgeeganage/05ca784c7c0c1c061e5ed25e4c6eb84c to your computer and use it in GitHub Desktop.
Every method
// Signature of the callback
type CallBackEvery<T> = (
value: T,
index?: number,
collection?: T[]
) => Promise<boolean>;
/**
* Async Every function
*
* @export
* @template T
* @param {T[]} elements
* @param {CallBackEvery<T>} cb
* @returns {Promise<boolean>}
*/
async function aEvery<T>( elements: T[], cb: CallBackEvery<T> ): Promise<boolean> {
for (const [index, element] of elements.entries()) {
if (!(await cb(element, index, elements))) {
return false;
}
}
return true;
}
// You can use as follows
const array = [1, 2, 3, 4];
const output = await aEvery<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