Javascript 异步存储未定义检查

Javascript 异步存储未定义检查,javascript,react-native,expo,Javascript,React Native,Expo,如何确定异步存储是否处于 错误拒绝 if (await AsyncStorage.getItem("id") == undefined){ alert("yes"); } 如果您正在尝试检查失败的getItem调用(我在simulator上遇到过,必须重新安装应用程序才能再次工作),您可以尝试将代码包装在try catch中,方法如下: try { if (await AsyncStorage.getItem("id") == undefined){ alert

如何确定异步存储是否处于

错误拒绝

if (await AsyncStorage.getItem("id") == undefined){
    alert("yes");
}

如果您正在尝试检查失败的getItem调用(我在simulator上遇到过,必须重新安装应用程序才能再次工作),您可以尝试将代码包装在try catch中,方法如下:

try {
    if (await AsyncStorage.getItem("id") == undefined){
        alert("item not found ");
}
} catch(error) {
    console.log("getItem function failed", error)
}

实现这一目标的更优雅的方法是:

const id = await AsyncStorage.getItem("id")   // Get the item reference of ID if exists

if (id) {
   // Do what you want if ID is actually there
} else {
   // Do what you want if ID is not there
}
可能是重复的: