I am currently investigating whether I can convert a Promise rejection into a typed Observable.
Within my login component, I have a function that looks like this...
login() {
const login$ = this.authenticationService.login(this.loginForm.value)
login$
.pipe(
finalize(() => {
this.loginForm.markAsPristine();
this.isLoading = false;
}),
untilDestroyed(this)
)
.subscribe(
(credentials) => {
log.debug(`${credentials.username} successfully logged in`);
this.router.navigate([this.route.snapshot.queryParams.redirect || '/'], { replaceUrl: true });
},
(error) => {
log.debug(`Login error: ${error}`);
this.error = error;
}
)
}
and the authentication service includes a method similar to this...
login(context: LoginContext): Observable<Credentials> {
var data = {} as Credentials
// Invoking a method that returns a Promise
this.server.authenticate(LoginContext)
.then((result) -> {
data.username = result.user.username
data.token = result.accessToken
this.credentialsService.setCredentials(data, context.remember)
// Uncertain about how to return data as an Observable here
// return data
})
.catch((err) => {
// Also unsure of how to handle errors and bubble them up for subscription
// return throwError(err)
})
}
Is it possible to return an Observable<Credentials>
when the Promise resolves, and pass up the error if it rejects?
I understand that changing the authentication service function to return
Promise<Observable<Credentials>>
would solve the issue, but I am keen on exploring new solutions. Any insights on achieving this are greatly appreciated.