2017-06-02 75 views
2

我想在方法中等待,直到承諾返回。等待承諾完成並返回true或false

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方法的方法只是想知道登錄是否成功。我嘗試了很多東西,但沒有成功。

任何人都可以幫忙。非常感謝 乾杯

回答

5

你只需要將承諾鏈接到另一個then。 調用此方法:

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

更多關於promise chaining

+0

THX了很多的工作完全按照我的願望。 –

3

可以使用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; 
    }); 
} 

loginOffline('myname', 'mypassword').then((result) => { 
    if(result) { 
     // true was returned 
    } else { 
     // false was returned 
    } 
}) 
+0

[這是一個反模式](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) –

+0

@suraj I看看你給我的鏈接並編輯答案。如果我能正確理解,你可以請看看並告訴我。如果我錯了,請隨時編輯並糾正我。 – Dhyey

+0

你不會有'resolve(bool)',因爲它不是一個參數了......只是'return boolean_value' .. –

0

,你可以在這裏找到解決方案:

function GetUserPemission(){ 
    return new Promise((resolve,reject)=>{ 
    getdata().then((data)=>{ 
     if(data){ 
      resolve(true); 
     }else{ 
     resolve(false); 
     } 
    }).catch((error)=>{ 
      resolve(false); 
    }) 
    }) 
} 

function getdata(){ 
return new Promise((resolve,reject)=>{ setTimeout(()=>{resolve("Data")},100)}) 
} 

GetUserPemission().then((data)=>{ 
     alert(data); 
}); 
+1

正如我在其他評論中提到的那樣。每次包裝新承諾是[反模式](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern -and-怎麼辦-I-避免-IT) –

1

的從@suraj回答是正確的,只需使用另一個.then()圍繞返回的結果,但替代方法是使用關鍵字await/async來重寫代碼。

public async loginOffline(username: string, password: string) { 
    try { 
     this.userLogin = await this.database.getItem('currentUser', 'login'); 
    } catch(e) { 
     return false; 
    } 
    console.log(username + ' ' + this.userLogin.username + ' ' + password + ' ' + this.userLogin.password); 
    return (
     username === this.userLogin.username && 
     password === this.userLogin.password); 
    } 

// This code must also be inside a function marked as 'async': 
/********* call the method and descide what to do **************/ 
if(await loginOffline('myname', 'mypassword'){ 
// Do something..... 
} else { 
// Do something else ..... 
} 
...... 

注意,每async功能將自動轉換成任何它返回到一個承諾,所以你必須使用async一路上漲調用樹。或者,如果由於某種原因,你不想做async你可以使用then的結果作爲前來電:

// If the caller cannot be marked `async`: 
/********* call the method and descide what to do **************/ 
loginOffline('myname', 'mypassword').then(result => { 
    if (result) { 
    // Do something..... 
    } else { 
    // Do something else ..... 
    } 
}); 
......