Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait till the promise is finished and return true or false

I want to wait within a method till a promise return.

public loginOffline(username: string, password: string) {
    return this.database.getItem('currentUser', 'login').then(data => {
        this.userLogin = data;
        console.log(username + ' ' + this.userLogin.username + ' ' + password + ' ' + this.userLogin.password);
        if (username === this.userLogin.username && password === this.userLogin.password) {
            return true;
        } else {
            return false;
        }
    }).catch(err => {
        return false;
    });

}

/********* call the method and descide what to do **************/
if (loginOffline('myname', 'mypassword'){
    // Do something.....
} else {
    // Do something else .....
}
......

this doesn't work. The method to call this loginOffline method just want to know if the login in was successful. I tried many things but nothing worked out.

Can anybody help. Thanks a lot Cheers

like image 354
M. Fish Avatar asked Dec 03 '22 13:12

M. Fish


1 Answers

You just have to chain the promise with another then. Call this way:

loginOffline('myname', 'mypassword').then(result =>{
   if(result){
     //do something
   }else{
     //do something else
   }
})

More on promise chaining

like image 104
Suraj Rao Avatar answered Dec 06 '22 10:12

Suraj Rao