I am performing several queries simultaneously and creating an array of promises from them. I want the resulting array to only have certain properties returned from the query, and I want to filter others (but these operations could really be anything). Currently, I have:
Promise.all(Ids.map(Id => query(Id)
.then(result => result.Item)
)).then(results => results.filter(Boolean));
One way is to use Reactive Extensions library, which provides a way to integrate different models for asynchronicity into a conceptual "stream" model. E.g. the code you have up there (assuming
Ids is an array and
query returns a promise) would turn into this:
Observable.from(Ids) .flatMap(id => query(id)) .map(result => result.Item) .filter(item => item.filterByThis) // Not sure what Boolean is supposed to be in your code .toArray();
Ids is an
number, we start out by producing an
Observable<number>, then pass it through various familiar looking operators to transform and filter the stream, then finally use
toArray get an observable that emits a single array of whatever type
result.Item is supposed to be.
You can think of Observables as a generalization of the concept of promises to asynchronous operations that return any number of values, rather than just one. This is a powerful paradigm, and the Rx library is pretty popular in many languages. You can read more about it here or get started with this npm package.