Skip to content

Commit

Permalink
fix(coreservices): Use Promise.reject()/resolve()/all()
Browse files Browse the repository at this point in the history
closes #2683
  • Loading branch information
christopherthielen committed Apr 8, 2016
1 parent 0742d25 commit 62b2ebc
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/justjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {extend, assertPredicate, forEach, applyPairs} from "./common/common";

/** $q-like promise api */
services.$q = (executor: (resolve, reject) => void) => new Promise(executor);
services.$q.when = (val) => new Promise((resolve, reject) => resolve(val));
services.$q.reject = (val) => new Promise((resolve, reject) => { reject(val); });
services.$q.when = (val) => Promise.resolve(val);
services.$q.reject = (val) => Promise.reject(val);
services.$q.defer = function() {
let deferred: any = {};
deferred.promise = new Promise((resolve, reject) => {
Expand All @@ -24,11 +24,7 @@ services.$q.defer = function() {

services.$q.all = function (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))), services.$q.when())
.then(() => { resolve(results); }, reject);
});
return Promise.all(promises);
}

if (isObject(promises)) {
Expand All @@ -38,6 +34,7 @@ services.$q.all = function (promises: { [key: string]: Promise<any> } | Promise<
// 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 services.$q.all(chain).then(values => {
let value = values.reduce((acc, tuple) => { acc[tuple.key] = tuple.val; return acc; }, {});
Expand Down

0 comments on commit 62b2ebc

Please sign in to comment.