fix(facade): Make PromiseWrapper#all semantics equivalent

The semantics between ES6 `Promise#all` and Dart's `Future#wait` are
different for values that are not `Promise`/`Future`s. In ES6,
non-`Promise` values are immediately completed to their current value.
In Dart, non-`Future` values cause an error.

Updated Dart's `PromiseWrapper#all` implementation to conform to the ES6
spec.
This commit is contained in:
Tim Blasi 2015-05-29 17:09:59 -07:00
parent cd52d8a3be
commit 22f5925202
3 changed files with 50 additions and 5 deletions

View File

@ -9,9 +9,12 @@ class PromiseWrapper {
static Future reject(obj, stackTrace) => new Future.error(obj,
stackTrace != null ? stackTrace : obj is Error ? obj.stackTrace : null);
static Future<List> all(List<Future> promises) => Future.wait(promises);
static Future<List> all(List<dynamic> promises) {
return Future
.wait(promises.map((p) => p is Future ? p : new Future.value(p)));
}
static Future then(Future promise, success(value), Function onError) {
static Future then(Future promise, success(value), [Function onError]) {
if (success == null) return promise.catchError(onError);
return promise.then(success, onError: onError);
}

View File

@ -18,13 +18,13 @@ export class PromiseWrapper {
return promise.catch(onError);
}
static all(promises: List<Promise<any>>): Promise<any> {
static all(promises: List<any>): Promise<any> {
if (promises.length == 0) return Promise.resolve([]);
return Promise.all(promises);
}
static then<T>(promise: Promise<T>, success: (value: any) => T | Thenable<T>,
rejection: (error: any, stack?: any) => T | Thenable<T>): Promise<T> {
rejection?: (error: any, stack?: any) => T | Thenable<T>): Promise<T> {
return promise.then(success, rejection);
}

View File

@ -14,6 +14,7 @@ import {
} from 'angular2/test_lib';
import {ObservableWrapper, EventEmitter, PromiseWrapper} from 'angular2/src/facade/async';
import {ListWrapper} from 'angular2/src/facade/collection';
export function main() {
describe('EventEmitter', () => {
@ -62,4 +63,45 @@ export function main() {
// should call dispose on the subscription on throw
// should call dispose on the subscription on return
});
}
// See ECMAScript 6 Spec 25.4.4.1
describe("PromiseWrapper", () => {
describe("#all", () => {
it("should combine lists of Promises", inject([AsyncTestCompleter], (async) => {
var one = PromiseWrapper.completer();
var two = PromiseWrapper.completer();
var all = PromiseWrapper.all([one.promise, two.promise]);
var allCalled = false;
PromiseWrapper.then(one.promise, (_) => {
expect(allCalled).toBe(false);
two.resolve('two');
});
PromiseWrapper.then(all, (_) => {
allCalled = true;
async.done();
});
one.resolve('one');
}));
ListWrapper.forEach([null, true, false, 10, 'thing', {}, []], (abruptCompletion) => {
it(`should treat "${abruptCompletion}" as an "abrupt completion"`,
inject([AsyncTestCompleter], (async) => {
var one = PromiseWrapper.completer();
var all = PromiseWrapper.all([one.promise, abruptCompletion]);
PromiseWrapper.then(all, (val) => {
expect(val[1]).toEqual(abruptCompletion);
async.done();
});
one.resolve('one');
}));
});
});
});
}