我有一个从钥匙串(keychain)异步检索密码的函数。
在 MyStaticClass 中
static getPassword() {
Keychain
.getGenericPassword()
.then(function(credentials) {
console.log('Credentials successfully loaded for user ' + credentials.username);
return credentials.password
}).catch(function(error) {
console.log('Keychain couldn\'t be accessed! Maybe no value set?', error);
});
}
我尝试调用该函数并使用此代码接收 promise ,但结果未定义
MyStaticClass.getPassword().then((data) => {
alert(data);
});
我也尝试过这个代码,密码也未定义
static async login(){
const password = await MyStaticClass.getPassword();
alert(password);
}
最佳答案
getPassword 方法中缺少 return 语句
return Keychain ...
关于javascript - 使用 React Native 创建异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48203309/