Skip to content

Instantly share code, notes, and snippets.

@eschwartz
Last active November 17, 2016 22:30
Show Gist options
  • Save eschwartz/1afb54b2608920327449036f790aa707 to your computer and use it in GitHub Desktop.
Save eschwartz/1afb54b2608920327449036f790aa707 to your computer and use it in GitHub Desktop.
Filter array, using asyncronous logic
/**
* @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