【发布时间】:2019-05-20 19:58:52
【问题描述】:
我有以下代码:
this.somePromiseFn<T> // this fn is actually a promise
.then(res => res as T)
.catch(err => this.handleError<T>(err));
handleError<T>(err: HttpErrorResponse) {
// do something here
// and return like
return {} as T;
}
上面的代码工作正常。但是我如何在不使用另一个回调函数的情况下将err 参数直接传递给handleError 方法。由于catch 默认将err 传递给回调函数,它也应该将err 传递给handleCatchError。我想要类似的东西:
this.somePromiseFn<T>
.then(res => res as T)
.catch(this.handleCatchError<T>); // this line gives error 'An argument for 'err' was not provided'
但上面的代码会产生错误:
预期有 1 个参数,但得到了 0。
没有提供 'err' 的参数。
虽然我已经访问过以下问题:
但上述问题也建议使用另一个回调函数。
【问题讨论】:
-
这似乎不是 minimal reproducible example,除非您在对其中一个答案的评论中计算 stackblitz 链接。
this.someFn().then((r) => console.log(r)).catch(this.cb);对你有用吗?如果不是,请编辑此问题以构成可重现的问题。祝你好运!
标签: angular typescript