Last active
November 17, 2016 22:30
-
-
Save eschwartz/1afb54b2608920327449036f790aa707 to your computer and use it in GitHub Desktop.
Filter array, using asyncronous logic
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
/** | |
* @param {Array<T>} list | |
* @param {(T):Promise<Boolean>} filter | |
* @returns {Promise<T[]>} | |
*/ | |
function filterAsync(list, filter) { | |
return Promise | |
.all( | |
list.map(item => filter(item) | |
.then(filterRes => ({ filterRes, item })) | |
) | |
) | |
.then(filterResults => filterResults | |
.filter(({ item, filterRes }) => filterRes) | |
.map(({ item, filterRes }) => item) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment