Expo AsyncStorage 返回承诺问题

Expo AsyncStorage returning promise issue

我是 react-native 和使用 Expo 的新手。 我想在连接或使用应用程序时从用户及其设备获取信息,例如设备 ID、制造商、os 是什么等。 我已经创建了一个文件,其中放置了所有 AsyncStorage 函数。

export async function storeDeviceUID(uid) {
  // TODO : Do validation
  try {
    await AsyncStorage.setItem("@App:deviceUID" , JSON.stringify(deviceUID));
  } catch (error) {
    // Error saving data
    console.log("KO");
  }

  return uid;
}

export async function retrieveDeviceUID() {
  try {
    const value = await AsyncStorage.getItem("@App:deviceUID");
    if (value !== null) {
      return value;
    }
  } catch (error) {
    // Error saving data
    //console.log("KO");
  }
}

export async function storeDeviceManufacturer(value) {
  //TODO : Do validation
  try {
    await AsyncStorage.setItem("@App:device_manufacturer", value);
  } catch (error) {
    // Error saving data
    //console.log("KO");
  }
}

export async function retrieveDeviceManufacturer() {
  try {
    const value = await AsyncStorage.getItem("@App:device_manufacturer");
    if (value !== null) {
      return value;
    }
  } catch (error) {
    // Error saving data
    //console.log("KO");
  }
}

export async function storeDeviceOSVersion(value) {
  //TODO : Do validation
  try {
    await AsyncStorage.setItem("@App:device_osVersion", value);
  } catch (error) {
    // Error saving data
    //console.log("KO");
  }
}

export async function retrieveDeviceOSVersion() {
  try {
    const value = await AsyncStorage.getItem("@App:device_osVersion");
    if (value !== null) {
      return value;
    }
  } catch (error) {
    // Error saving data
    //console.log("KO");
  }
}

当我调用其他文件中的函数时:

  render() {
    const viewStyles = [styles.container, { backgroundColor: "orange" }];
    const textStyles = {
      color: "white",
      fontSize: 40,
      fontWeight: "bold"
    };

    // User active session
    if (retrieveProfileUserId() !== null && retrieveProfileUserId() > 0) {
      // OK
    } else {
      // Need connection
    }
    console.log("===>retrieveProfileUserId", retrieveProfileUserId());

    // Device UUID
    console.log("Results retrieveDeviceUID() : ", retrieveDeviceUID());
    if (retrieveDeviceUID() !== null) {
      // OK
    } else {
      // TODO : next step...
      // storeDeviceUID(getDeviceUID());
    }

    // Detect Manufacturer : iOS, Android, ..
    if (retrieveDeviceManufacturer() !== null) {
      // OK
    } else {
      storeDeviceManufacturer(getDeviceManufacturer());
    }

    // Get system version
    if (retrieveDeviceOSVersion() !== null) {
      // OK
    } else {
      storeDeviceOSVersion(getDeviceOSVersion());
    }

    console.log("Results retrieveDeviceOSVersion() : ", retrieveDeviceOSVersion());
    console.log("Results retrieveDeviceManufacturer() : ", retrieveDeviceManufacturer());

我收到错误:

===>retrieveProfileUserId Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
}
Results retrieveDeviceUID() :  Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
}
Results retrieveDeviceOSVersion() :  Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
}
Results retrieveDeviceManufacturer() :  Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
}

我想我有点 lost,我需要你的帮助。这对你来说可能看起来很愚蠢,但......真的在努力学习。 感谢阅读我

使用异步函数时首先使用 await

第二次使用 componentDidMount 从异步函数中获取结果

不要在渲染中使用 await

class SampleClass extends React.Component {
  constructor () {
    this.state = {
        userId:null,
    };
  }

componentDidMount= async()=> {
   const userId=await retrieveDeviceManufacturer();
   this.setState({userId}); 
  }

render () {
    const { userId }=this.state;
   }
}