feat(zone.js): support Promise.allSettled (#31849)

PR Close #31849
This commit is contained in:
JiaLiPassion
2019-07-25 23:13:05 +09:00
committed by Alex Rickabaugh
parent 2a6e6c02ed
commit 96cbcd6da4
2 changed files with 131 additions and 8 deletions

View File

@ -286,7 +286,20 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
return promise;
}
static all<R>(values: any): Promise<R> {
static all<R>(values: any): Promise<R> { return ZoneAwarePromise.allWithCallback(values); }
static allSettled<R>(values: any): Promise<R> {
const P = this && this.prototype instanceof ZoneAwarePromise ? this : ZoneAwarePromise;
return P.allWithCallback(values, {
thenCallback: (value: any) => ({status: 'fulfilled', value}),
errorCallback: (err: any) => ({status: 'rejected', reason: err})
});
}
static allWithCallback<R>(values: any, callback?: {
thenCallback: (value: any) => any,
errorCallback: (err: any) => any
}): Promise<R> {
let resolve: (v: any) => void;
let reject: (v: any) => void;
let promise = new this<R>((res, rej) => {
@ -305,13 +318,29 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
}
const curValueIndex = valueIndex;
value.then((value: any) => {
resolvedValues[curValueIndex] = value;
unresolvedCount--;
if (unresolvedCount === 0) {
resolve !(resolvedValues);
}
}, reject !);
try {
value.then(
(value: any) => {
resolvedValues[curValueIndex] = callback ? callback.thenCallback(value) : value;
unresolvedCount--;
if (unresolvedCount === 0) {
resolve !(resolvedValues);
}
},
(err: any) => {
if (!callback) {
reject !(err);
} else {
resolvedValues[curValueIndex] = callback.errorCallback(err);
unresolvedCount--;
if (unresolvedCount === 0) {
resolve !(resolvedValues);
}
}
});
} catch (thenErr) {
reject !(thenErr);
}
unresolvedCount++;
valueIndex++;