Skip to content

Commit 16220db

Browse files
refactor($q): use Promise.all() instead of custom .all implementation
1 parent b07a24b commit 16220db

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

src/vanilla/$q.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,15 @@ export const $q = {
4040
/** Like Promise.all(), but also supports object key/promise notation like $q */
4141
all: (promises: { [key: string]: Promise<any> } | Promise<any>[]) => {
4242
if (isArray(promises)) {
43-
return new Promise((resolve, reject) => {
44-
let results = [];
45-
promises.reduce((wait4, promise) => wait4.then(() => promise.then(val => results.push(val))), $q.when())
46-
.then(() => { resolve(results); }, reject);
47-
});
43+
return Promise.all(promises);
4844
}
4945

5046
if (isObject(promises)) {
5147
// Convert promises map to promises array.
5248
// When each promise resolves, map it to a tuple { key: key, val: val }
5349
let chain = Object.keys(promises)
5450
.map(key => promises[key].then(val => ({key, val})));
51+
5552
// Then wait for all promises to resolve, and convert them back to an object
5653
return $q.all(chain).then(values =>
5754
values.reduce((acc, tuple) => { acc[tuple.key] = tuple.val; return acc; }, {}));

0 commit comments

Comments
 (0)