Created
July 4, 2020 08:50
-
-
Save dagda1/5590ba52107ea1d5b9cb35b4edb724d6 to your computer and use it in GitHub Desktop.
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
type CallBackFN<T, R> = (value: T, index: number, array: T[]) => Promise<R>; | |
export const flatten = <T>(arr: T[][]): T[] => { | |
return arr.reduce((acc, value) => acc.concat(value), [] as T[]); | |
}; | |
export const asyncMap = <T, R>(arr: T[], asyncFn: CallBackFN<T, R>): Promise<R[]> => { | |
return Promise.all(arr.map(asyncFn)); | |
}; | |
export const asyncFlatMap = async <T, R>(arr: T[], asyncFn: CallBackFN<T, R[]>): Promise<R[]> => { | |
return Promise.all(flatten(await asyncMap(arr, asyncFn))); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment