Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 7def5a0

Browse files
committed
fix(bluebird): fix #1112, bluebird chained callback should return a Bluebird Promise
1 parent 34c12e5 commit 7def5a0

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

lib/common/promise.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,6 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
476476

477477
if (NativePromise) {
478478
patchThen(NativePromise);
479-
480-
/*let fetch = global['fetch'];
481-
if (typeof fetch == 'function') {
482-
global['fetch'] = zoneify(fetch);
483-
}*/
484479
}
485480

486481
// This is not part of public API, but it is useful for tests, so we expose it.

lib/extra/bluebird.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) =
2525
args[i] = function() {
2626
const argSelf: any = this;
2727
const argArgs: any = arguments;
28-
zone.scheduleMicroTask('Promise.then', () => {
29-
return func.apply(argSelf, argArgs);
28+
return new Bluebird((res: any, rej: any) => {
29+
zone.scheduleMicroTask('Promise.then', () => {
30+
try {
31+
res(func.apply(argSelf, argArgs));
32+
} catch (error) {
33+
rej(error);
34+
}
35+
});
3036
});
3137
};
3238
}

test/extra/bluebird.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
// this spec will not be integrated with Travis CI, because I don't
1010
// want to add bluebird into devDependencies, you can run this spec
1111
// on your local environment
12+
process.on('unhandledRejection', (reason, p) => {
13+
console.log('Unhandled Rejection at:', p, 'reason:', reason);
14+
// application specific logging, throwing an error, or other logic here
15+
});
1216

1317
describe('bluebird promise', () => {
1418
let BluebirdPromise: any;
@@ -636,4 +640,42 @@ describe('bluebird promise', () => {
636640
});
637641
});
638642
});
643+
644+
it('should be able to chain promise', (done: DoneFn) => {
645+
Zone.current.fork({name: 'zone_A'}).run(() => {
646+
new BluebirdPromise((resolve: any, reject: any) => {
647+
expect(Zone.current.name).toEqual('zone_A');
648+
resolve(1);
649+
})
650+
.then((r: any) => {
651+
expect(r).toBe(1);
652+
expect(Zone.current.name).toEqual('zone_A');
653+
return Promise.resolve(2);
654+
})
655+
.then((r: any) => {
656+
expect(r).toBe(2);
657+
expect(Zone.current.name).toEqual('zone_A');
658+
});
659+
});
660+
Zone.current.fork({name: 'zone_B'}).run(() => {
661+
new BluebirdPromise((resolve: any, reject: any) => {
662+
expect(Zone.current.name).toEqual('zone_B');
663+
reject(1);
664+
})
665+
.then(
666+
() => {
667+
fail('should not be here.');
668+
},
669+
(r: any) => {
670+
expect(r).toBe(1);
671+
expect(Zone.current.name).toEqual('zone_B');
672+
return Promise.resolve(2);
673+
})
674+
.then((r: any) => {
675+
expect(r).toBe(2);
676+
expect(Zone.current.name).toEqual('zone_B');
677+
done();
678+
});
679+
});
680+
});
639681
});

0 commit comments

Comments
 (0)