[英]Call function after currentUserSync - Async Await issue
我正在使用react-native-google-signin 。 我的代碼是:
async _setupGoogleSignin() {
try {
await GoogleSignin.hasPlayServices({ autoResolve: true });
await GoogleSignin.configure({
webClientId: '<from web>',
offlineAccess: true
});
const user = await GoogleSignin.currentUserAsync()
.then(this._someFunction(user)); // Is this correct?
console.log(user); // this works. User is logged
}
catch(err) {
console.log("Play services error", err.code, err.message);
}
}
_someFunction(user){
console.log("ID: ",user.id) // Error is thrown here
this.setState({id: user.id}); // This is not set
}
隨着.then(this._someFunction(user));
,我想將user
傳遞給函數_someFunction
。
錯誤是Play services error undefined Cannot read property 'id' of undefined
。
我希望能夠在GoogleSignin.currentUserAsync()
完成時調用設置user
的函數。 我究竟做錯了什么?
async..await
與常規的async..await
代碼混合在一起,而且不是很好。
.then(this._someFunction(user))
無效,因為then
需要一個函數作為參數,並且它接收undefined
。 此外,此時未定義user
。
它應該是
const user = await GoogleSignin.currentUserAsync();
this._someFunction(user);
這正是async..await
的用途。 在可行的情況下,將函數展平並避免then
s。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.