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

Commit 6ba3169

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(bluebird): fix#1112, bluebird chained callback should return a Bluebird Promise (#1114)
1 parent 49e0548 commit 6ba3169

4 files changed

Lines changed: 175 additions & 8 deletions

File tree

‎file-size-limit.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"path": "dist/zone.min.js",
55
"checkTarget": true,
6-
"limit": 42000
6+
"limit": 42050
77
}
88
]
99
}

‎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: 18 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
constargSelf: any=this;
2727
constargArgs: any=arguments;
28-
zone.scheduleMicroTask('Promise.then',()=>{
29-
returnfunc.apply(argSelf,argArgs);
28+
returnnewBluebird((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
}
@@ -35,6 +41,16 @@ Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) =
3541
});
3642
});
3743

44+
Bluebird.onPossiblyUnhandledRejection(function(e: any,promise: any){
45+
try{
46+
Zone.current.runGuarded(()=>{
47+
throwe;
48+
});
49+
}catch(err){
50+
api.onUnhandledError(err);
51+
}
52+
});
53+
3854
// override global promise
3955
global[api.symbol('ZoneAwarePromise')]=Bluebird;
4056
};

‎test/extra/bluebird.spec.ts‎

Lines changed: 156 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
letBluebirdPromise: any;
@@ -636,4 +640,156 @@ 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+
newBluebirdPromise((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+
returnPromise.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+
newBluebirdPromise((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+
returnPromise.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+
});
681+
682+
it('should catch rejected chained bluebird promise',(done: DoneFn)=>{
683+
constlogs: string[]=[];
684+
constzone=Zone.current.fork({
685+
name: 'testErrorHandling',
686+
onHandleError: function(){
687+
// should not get here
688+
logs.push('onHandleError');
689+
returntrue;
690+
}
691+
});
692+
693+
zone.runGuarded(()=>{
694+
returnBluebirdPromise.resolve()
695+
.then(()=>{
696+
thrownewError('test error');
697+
})
698+
.catch(()=>{
699+
expect(logs).toEqual([]);
700+
done();
701+
});
702+
});
703+
});
704+
705+
it('should catch rejected chained global promise',(done: DoneFn)=>{
706+
constlogs: string[]=[];
707+
constzone=Zone.current.fork({
708+
name: 'testErrorHandling',
709+
onHandleError: function(){
710+
// should not get here
711+
logs.push('onHandleError');
712+
returntrue;
713+
}
714+
});
715+
716+
zone.runGuarded(()=>{
717+
returnPromise.resolve()
718+
.then(()=>{
719+
thrownewError('test error');
720+
})
721+
.catch(()=>{
722+
expect(logs).toEqual([]);
723+
done();
724+
});
725+
});
726+
});
727+
728+
it('should catch rejected bluebird promise',(done: DoneFn)=>{
729+
constlogs: string[]=[];
730+
constzone=Zone.current.fork({
731+
name: 'testErrorHandling',
732+
onHandleError: function(){
733+
// should not get here
734+
logs.push('onHandleError');
735+
returntrue;
736+
}
737+
});
738+
739+
zone.runGuarded(()=>{
740+
returnBluebirdPromise.reject().catch(()=>{
741+
expect(logs).toEqual([]);
742+
done();
743+
});
744+
});
745+
});
746+
747+
it('should catch rejected global promise',(done: DoneFn)=>{
748+
constlogs: string[]=[];
749+
constzone=Zone.current.fork({
750+
name: 'testErrorHandling',
751+
onHandleError: function(){
752+
// should not get here
753+
logs.push('onHandleError');
754+
returntrue;
755+
}
756+
});
757+
758+
zone.runGuarded(()=>{
759+
returnPromise.reject(newError('reject')).catch(()=>{
760+
expect(logs).toEqual([]);
761+
done();
762+
});
763+
});
764+
});
765+
766+
it('should trigger onHandleError when unhandledRejection',(done: DoneFn)=>{
767+
constzone=Zone.current.fork({
768+
name: 'testErrorHandling',
769+
onHandleError: function(){
770+
setTimeout(done,100);
771+
returntrue;
772+
}
773+
});
774+
775+
zone.runGuarded(()=>{
776+
returnPromise.reject(newError('reject'));
777+
});
778+
});
779+
780+
it('should trigger onHandleError when unhandledRejection in chained Promise',(done: DoneFn)=>{
781+
constzone=Zone.current.fork({
782+
name: 'testErrorHandling',
783+
onHandleError: function(){
784+
setTimeout(done,100);
785+
returntrue;
786+
}
787+
});
788+
789+
zone.runGuarded(()=>{
790+
returnPromise.resolve().then(()=>{
791+
thrownewError('test');
792+
});
793+
});
794+
});
639795
});

0 commit comments

Comments
 (0)