A small ES6 module for filtering array which contains async functions:

1
2
3
4
5
6
7
8
9
export default (array, predicate) => {
 
    const data = Array.from(array);
 
    // Transform all the elements into an array of promises using the predicate as the promise
    return Promise.all(data.map((element, index) => predicate(element, index, data)))
        .then(result => data.filter((element, index) => result[index]))
 
}

Example usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import filterAsync from './array-filter-promises';
 
const files = [
 ".",
 "..",
 "_res",
 "tops",
 "highlights",
 "index.json",
 "schema.json"
];
 
filterAsync(files, path => {
    return promisify(fs.stat, path).then(stats => {
        if (path.indexOf('_') !== 0 &&
            path.indexOf('.') !== 0 &&
            stats.isDirectory()) return path;
    });
});
 
//-> [ "tops", "highlights" ]

Note: promisify() use my module that turns any async function with callback into a promise https://www.npmjs.com/package/promisify-callback