With a little thought you can write a generalised utility function that will take a start object and a filter sequence as its arguments, dynamically build the required .then
chain, and return a promise of the multi-filtered result.
The function will look like this ...
function doFilters(filterArray, startObj) {
return filterArray.reduce(function(promise, f) {
return promise.then(function(result) {
return filters.doFilter(f, result);
});
}, when(startObj));
}
... which is an adaptation of a pattern given here in the section headed "The Collection Kerfuffle".
For the operation you want, call as follows :
doFilters(['filter1name', 'filter2name', 'filter3name'], reqAndPosts).then(function(result) {
//All filtering is complete.
//Do awesome stuff with the result.
});
Provding it is not destroyed and is in scope, doFilters()
will remain available to be used elsewhere in your code :
doFilters(['f1', 'f2', 'f3'], myOtherObject).then(function(result) {
//...
});
With very little more effort, you could tidy things up by phrasing doFilters()
as a method of filters
. That would be best of all.