Search code examples
firebaseionic2loadingwaitionic3

How to wait value in Ionic 2


I'm trying to wait value that may change after execute "this.bookmarList.take(1).subscribe(..."

My problem is that when I run the application, its execute the second if statement "if(found==false)" before execute my subscribe.

This is my code you may found some thing wrong or need to add some additions to solve my problem.

let loader = this.loadingCtrl.create({
  content: "",
});
let found=false;
loader.present().then(()=>{
    this.bookmarkList.take(1).subscribe(data=>{
        data.forEach(b=>{
            if(b.nID==this.appService.id && b.url==url){
                let alert = this.alertCtrl.create({
                title: 'Already Bookmarked',
                    subTitle: 'This note image already bookmarked',
                    buttons: ['OK']
                });
                alert.present();
                found=true;
            }
        })
    })
}).then(()=>{
    if(found==false){
        this.afd.list('/bookmark/').push({
            uemail: this.userService.email,
            nID: this.appService.id,
            url: url,
            date: dateX
        });
        let alert = this.alertCtrl.create({
            title: 'Bookmarked Successfully',
            subTitle: 'This note image has been bookmarked successfully',
                buttons: ['OK']
        });
        alert.present();
    }
}).then(_=>{
    loader.dismiss();
})

Solution

  • I could find the solution

    let loader = this.loadingCtrl.create({
      content: "",
    });
    let found=false;
    loader.present().then(_=>{
        this.bookmarkList.take(1).subscribe(data=>{
            data.forEach(b=>{
                if(b.nID==this.appService.id && b.url==url){
                    let alert = this.alertCtrl.create({
                        title: 'Already Bookmarked',
                        subTitle: 'This note image already bookmarked',
                        buttons: ['OK']
                    });
                    alert.present();
                    found=true;
                }
            })
            if(found==false){
                this.afd.list('/bookmark/').push({
                    uemail: this.userService.email,
                    nID: this.appService.id,
                    url: url,
                    date: dateX
                });
                let alert = this.alertCtrl.create({
                    title: 'Bookmarked Successfully',
                    subTitle: 'This note image has been bookmarked successfully',
                    buttons: ['OK']
                });
                alert.present();
            }
            //return
        })
    }).then(_=>{
        loader.dismiss();
    })