【发布时间】:2021-11-06 22:22:28
【问题描述】:
我有这种情况:
controller.ts
methodA(): void {
myServive.someMethod()
.then( () => console.log("then") )
.catch( e => {
console.log("catch");
});
}
service.ts
someMethod(): ng:IPromise<void> {
const deferred = this.$q.defer<void>();
return this.OtherService.otherMethod()
.catch ( e => {
deferred.reject(reason);
}
}
其他服务.ts
otherMethod(): ng.IPromise<any> {
return this.HttpService.get(url);
}
测试:
- otherMethod (otherService.ts) 收到来自 HttpService 的错误。
- someMethod (service.ts) 中的 catch 已执行。
为什么在controller.ts中,then块被执行了?
【问题讨论】:
-
来自MDN:“如果 onRejected 抛出错误或返回本身被拒绝的 Promise,则 catch() 返回的 Promise 被拒绝;否则,它被解决。”
标签: javascript angularjs promise