我想在一个方法中等待直到 promise 返回。
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 .....
}
......
这行不通。调用此 loginOffline 方法的方法只是想知道登录是否成功。我尝试了很多事情,但没有成功。
谁能帮忙。非常感谢 干杯
最佳答案
您只需将 promise 与另一个 then
链接起来。
这样调用:
loginOffline('myname', 'mypassword').then(result =>{
if(result){
//do something
}else{
//do something else
}
})
更多关于 promise chaining
关于javascript - 等到 promise 完成后返回 true 或 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44322947/