HarmonyOS try catch是异步执行吗?

这边获取dic,try catch里面的aaid和versionCode都是在最后返回dic之后才获取到,要如何才能拿到之后再进行返回?

代码如下:

async function getHeaderDic() {
  try {
    let headerDic = new HashMap<string , string>()
    let dateStr = ''
    try {
      let time = systemDateTime.getTime(false)
      console.info('时间为:',time);
      dateStr = getTimeToYYYYDDMMHHMMSS(time)
      console.info('时间戳为:',dateStr);

    } catch(e) {
      console.info(`Failed to get currentTime. message: ${e.message}, code: ${e.code}`);
    }
    //获取版本号
    let versionCode = ''
    //  更改
    let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA;
    try {
      bundleManager.getBundleInfoForSelf(bundleFlags).then((data) => {
        versionCode = data.versionName.toString();
        console.info('versionCode版本号为:.',versionCode)
        headerDic.set('osVersion',versionCode);
      })
    } catch (err) {

    }
    let versionId = deviceInfo.versionId;
    headerDic.set('osType','01');
    headerDic.set('appVersion', versionId);
    let aaid = ''
    try {
      AAID.getAAID((err: BusinessError, data: string) => {
        if (err) {
          hilog.error(0x0000, 'Get AAID failed: %{public}d %{public}s', err.code, err.message);
        } else {
          hilog.info(0x0000, 'Get AAID successfully: %{public}s', data);
          aaid = data;
          headerDic.set('dvImei', aaid);
        }
      });
    } catch (err) {
      let e: BusinessError = err as BusinessError;
      hilog.error(0x0000, 'Get AAID catch error: %{public}d %{public}s', e.code, e.message);
    }
    let random18Str : number = Math.ceil((Math.pow(10, 18) - 1) * Math.random());
    console.info('生成的18位随机数字段.',random18Str.toString())
    headerDic.set('nonceStr',dateStr + random18Str.toString());
    headerDic.set('txnTm',dateStr);
    headerDic.set('appChlNo','04');
    console.info('abc1234,,,,,,',headerDic)
    return headerDic
  } catch (err) {

    console.info('abc1234',err)
    return ''
  }
}
HarmonyOS
2024-12-27 13:56:46
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

getBundleInfoForSelf和getAAID都是异步方法,可以使用 await 实现同步,如下:

  // 获取版本号
  let versionCode: string = ''
  // 更改
  let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA;
  try {
    let bundleInfo = await bundleManager.getBundleInfoForSelf(bundleFlags)
    versionCode = bundleInfo.versionName.toString();
    console.info('versionCode版本号为:.', versionCode)
    headerDic.set('osVersion', versionCode);
  } catch (err) {
  }
  let versionId: string = deviceInfo.versionId;
  headerDic.set('osType', '01');
  headerDic.set('appVersion', versionId);
  try {
    let aaid: string = await AAID.getAAID()
    if (aaid) {
      headerDic.set('dvImei', aaid);
    }
  } catch (err) {
    let e: BusinessError = err as BusinessError;
    hilog.error(0x0000, 'testTag', 'Get AAID catch error: %{public}d %{public}s', e.code, e.message);
  }
分享
微博
QQ
微信
回复
2024-12-27 16:56:44
相关问题
HarmonyOS catch error问题
393浏览 • 1回复 待解决
ArkTS异步机制与执行顺序
2046浏览 • 1回复 待解决
HarmonyOS h264/h265软解在CPU运行吗
232浏览 • 1回复 待解决
ArkTS 的异步编程模型如何实现的?
441浏览 • 0回复 待解决