将承诺转化为可观察的 [英] Convert promise to observable
问题描述
http和集合的可观察性似乎是直截了当的。我怎样才能将这样的东西转换为可观察的模式。
这是从我的服务组件,提供身份验证。我更愿意像Angular2中的其他http服务一样工作 - 支持数据,错误和完成处理。
firebase。 (函数(firebaseUser){
//做些什么来更新你的UI组件
//把用户对象传给UI组件
})。 catch(function(error){
// Handle Errors here。
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
这里的任何帮助将不胜感激。我唯一的替代解决方案是创建EventEmitters。但我想这是一个可怕的方式来做服务部分
试试这个:
var subscription = Observable.fromPromise(firebase.auth()。createUserWithEmailAndPassword(email,password));
subscription.subscribe(firebaseUser => / *对收到的数据做任何事情* /,
error => / *在这里处理错误* /);
您可以从fromPromise运算符中找到完整的引用这里
I am trying to wrap my head around observables. I love the way observables solve development and readablity issues. As I read, benefits are immense.
Observables on http and collections seem to be straight forward. How can I convert something like this to observable pattern.
This is from my service component, to provide authentication. I'd prefer this to work like other http services in Angular2 - with support for data, error and completion handlers.
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(firebaseUser) {
// do something to update your UI component
// pass user object to UI component
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
Any help here would be much appreciated. The only alternative solution I had was to create EventEmitters. But I guess that's a terrible way to do things in services section
try this:
var subscription = Observable.fromPromise(firebase.auth().createUserWithEmailAndPassword(email, password));
subscription.subscribe(firebaseUser => /* Do anything with data received */,
error => /* Handle error here */);
you can find complete reference to fromPromise operator here
这篇关于将承诺转化为可观察的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!