Article
home/blog/Loading

Filtering an array with a function that returns a Promise

You cannot filter an array using Array.prototype.filter using a promise as the function is synchronous and therefore does not support promises.

Using Promise.all()

const array: boolean[];

Promise.all(
    this.array.map((value: boolean) => {
        return Promise.resolve(value) // replace with your function
            .then((result) => result);
    })
).then((allResults: boolean[]) => {
    // set the results to something asynchronously
})

Alternativly using Bluebird Promises

If you use the Bluebird library for promises, you can use Promise.filter instead!

For example:

const array: boolean[];

const filtered = Promise.filter(
    array,
    Promise.resolve(true) // replace with something returning a promise
)