【发布时间】:2017-10-15 12:19:27
【问题描述】:
我是 Angular 的新手。
我正在开发一些身份验证功能,这些功能与 Promise 异步运行,并在这段代码中间做一些事情。 考虑到异步,我相信该函数应该返回一个 Observable。但是如何返回 Observable 和 promise 的结果/错误?
authentication.service.ts
loginwithProvider(): <????> {
this.auth.signInWithPopup(provider)
.then( result =>{
/* do some things, such as getting data and logging */
})
.catch( (error) => {
/* receive the error of the provider, do some things, such as logging */
})
return ????? <---- I want to return result and errors of the provider in an Observable to the caller function
}
login.component.ts
onLogin(){
this.request = this.authenticationService.loginWithProvider().subscribe(
(data) => {
console.log("onlogin - then success");
this.router.navigate(['/']);
},
(error) => {
console.log(" onlogin - error ", error);
show_error_to_user(error); <---- error from loginWithProvider
});
}
【问题讨论】:
-
这是与
angularfire2的巧合吗? -
是的,使用 angularfire2 版本 4 进行身份验证
标签: javascript angular promise observable