Skip to content

Commit

Permalink
refactor($q): use Promise.all() instead of custom .all implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Feb 23, 2017
1 parent b07a24b commit 16220db
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/vanilla/$q.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,15 @@ export const $q = {
/** Like Promise.all(), but also supports object key/promise notation like $q */
all: (promises: { [key: string]: Promise<any> } | Promise<any>[]) => {
if (isArray(promises)) {
return new Promise((resolve, reject) => {
let results = [];
promises.reduce((wait4, promise) => wait4.then(() => promise.then(val => results.push(val))), $q.when())
.then(() => { resolve(results); }, reject);
});
return Promise.all(promises);
}

if (isObject(promises)) {
// Convert promises map to promises array.
// When each promise resolves, map it to a tuple { key: key, val: val }
let chain = Object.keys(promises)
.map(key => promises[key].then(val => ({key, val})));

// Then wait for all promises to resolve, and convert them back to an object
return $q.all(chain).then(values =>
values.reduce((acc, tuple) => { acc[tuple.key] = tuple.val; return acc; }, {}));
Expand Down

0 comments on commit 16220db

Please sign in to comment.