I am currently working with the following code snippet:
this.somePromiseFn<T> // this function is actually a promise
.then(res => res as T)
.catch(err => this.handleError<T>(err));
handleError<T>(err: HttpErrorResponse) {
// perform actions here
// and return something like
return {} as T;
}
The code above functions correctly. However, I am trying to figure out how to directly pass the err
argument to the handleError
method without using an additional callback function. Since the catch
block inherently passes the err
to the callback function, it should also pass it to the handleCatchError
. My desired implementation looks like:
this.somePromiseFn<T>
.then(res => res as T)
.catch(this.handleCatchError<T>); // this line results in error 'An argument for 'err' was not provided'
However, upon running the above code, the following error message is displayed:
Expected 1 argument, but received 0.
An argument for 'err' was not provided.
Despite reviewing the following related questions:
- addEventListener calls the function without me even asking it to
- Javascript "addEventListener" Event Fires on Page Load
These references also recommend using an additional callback function.