Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/zone.js/lib/common/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,11 @@ export function patchPromise(Zone: ZoneType): void {

if (NativePromise) {
patchThen(NativePromise);
// TODO(atscott): Investigate generic to propagate any unknown properties
const nativeTry = (NativePromise as any)['try'];
if (nativeTry && typeof nativeTry === 'function') {
(ZoneAwarePromise as any)['try'] = nativeTry;
}
Comment thread
atscott marked this conversation as resolved.
patchMethod(global, 'fetch', (delegate) => zoneify(delegate));
}

Expand Down
33 changes: 33 additions & 0 deletions packages/zone.js/test/common/Promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,5 +969,38 @@ describe(
reject(error);
});
});

describe('Promise.try', () => {
it('should resolve', (done: DoneFn) => {
(Promise as any)
.try(() => 1)
.then((v: any) => {
expect(v).toBe(1);
done();
});
});
it('should reject on throw', (done: DoneFn) => {
const error = new Error('test');
(Promise as any)
.try(() => {
throw error;
})
.catch((e: any) => {
expect(e).toBe(error);
done();
});
});
it('should execute in the correct zone', (done: DoneFn) => {
const zone = Zone.current.fork({name: 'promise-try'});
zone.run(() => {
(Promise as any)
.try(() => 1)
.then(() => {
expect(Zone.current.name).toEqual(zone.name);
done();
});
});
});
});
}),
);
13 changes: 13 additions & 0 deletions packages/zone.js/test/test_fake_polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export function setupFakePolyfill(): void {
NativeError.customProperty = 'customProperty';
NativeError.customFunction = function () {};

// Polyfill Promise.try for testing pass-through
if (global.Promise && typeof global.Promise.try !== 'function') {
global.Promise.try = function (callback: any) {
return new global.Promise((resolve: any, reject: any) => {
try {
resolve(callback());
} catch (e) {
reject(e);
}
});
};
}

// add fake cordova polyfill for test
const fakeCordova = function () {};

Expand Down