链接可观察和承诺

时间:2017-08-05 21:58:00

标签: javascript angular firebase angularfire

我正在尝试使用AngularFire更新用户的个人资料图片。我使用返回put的{​​{1}}方法。在promise内,我使用promise更改用户信息中的图片observable。最后,我将图片URL元素的图片URL更改为用户信息中的图片DOM。问题是我的URL尝试在promise完成更新之前获取用户的图片URL。有没有办法将observable链接到observable,以便promise仅在promise完成时才会继续? 这是我的代码:

authService.ts

observable

user.component.ts

updatePicture(profilePicture){
    //first put the pictuer in the storage
    return storageRef.put(profilePicture)
           .then(snapshot => {
               downloadURL = snapshot.downloadURL;
           })
               //this is where I use the observable to update user info
           .then(() => {
               this.getUserAuth().subscribe(userAuth => {
                   userAuth.updateProfile({
                       displayName:userAuth.displayName,
                       photoURL:downloadURL
                   })
               })
           })
}

1 个答案:

答案 0 :(得分:0)

尝试将observable包装到new promise中,然后在你的承诺链中使用它:

var obs = new Observable(...);
var promise = new Promise((resolve, reject) => {
    obs.subscribe(your_on_next, reject, resolve);
});

这应该会给你一个在观察者终止时解决的承诺。现在,您可以在需要的时间点在承诺链中使用该承诺。只需从该链中的另一个then()返回。

您需要添加一些额外的代码,以便从链接下游需要的任何可观察内容中获取任何内容。