/** * The async variant of the Array.filter() method that * @param values An iterable of elements to filter * @param callbackFn The async callback that will be executed on the elements * @returns Promise */ export const filterAsync = async (values: Iterable, callbackFn: (entry: T) => Promise) => { const returns = [] for (const value of values) { ;(await callbackFn(value)) && returns.push(value) } return returns } declare global { /** * Defines an array of elements */ export interface Array { /** * Returns a promise with a new array of elements that meets the specified async callback */ filterAsync: (callbackFn: (entry: T) => Promise) => Promise } } ;(Array.prototype as any).filterAsync = function (callbackFn: (entry: any) => Promise) { return filterAsync(this, callbackFn) }