简体   繁体   English

Promise、async、await - 布尔值返回 false 但如果条件仍然被调用

[英]Promise, async, await - Boolean returns false but if condition still gets called

Context语境

I have this piece of code to force app update, that checks whether an update is needed and is supposed to return true or false depending on the version number of the user's app vs the App Store version.我有这段代码来强制应用程序更新,它检查是否需要更新,并且应该根据用户应用程序的版本号与 App Store 版本返回 true 或 false。 This seems to be working correctly when the version number of the user's app is lower , because it stores the right url in the website variable:当用户应用程序的版本号较低时,这似乎可以正常工作,因为它将正确的 url 存储在website变量中:

  let website = ''
  const isLatestVersion = async (): Promise<boolean> => {
    await VersionCheck.needUpdate({
      packageName: 'com.tfp.numberbomb',
    }).then((res) => {
      if (res.isNeeded) {
        website = res.storeUrl
        return true // open store if update is needed.
      } else return false
    })
  }

Problem问题

It doesn't work correctly when the version number of the user's app is equal/higher .当用户应用程序的版本号是equal/higher时,它不能正常工作。 This is the function where I am using the function from above.这是我使用上面函数的函数。 For some reason, the logic executes showPopup() even if the response from the function is false.出于某种原因,即使函数的响应为假,逻辑也会执行showPopup() What am I doing wrong?:我究竟做错了什么?:

  const handleBiometricLogin = async () => {
    if (!(await isLatestVersion())) {
      await showPopup()
      return
    }
    ... // rest of the logic that doesn't run even when it's supposed to because of the return statement

Additional info附加信息

showPopup() function just for reference: showPopup()函数仅供参考:

const showPopup = async () => {
    Alert.alert(
      translations['errors.login.update.title'] ?? 'Download Available',
      translations['errors.login.update.body'] ??
        'There is a new download available on the app store. Please update to gain access',
      [
        { text: translations['form.no-answer'] ?? 'No' },
        {
          text: translations['form.yes-answer'] ?? 'Yes',
          onPress: () => handleOpenLink(website, translations),
        },
      ],
    )
  }

I feel like I am doing the async and await stuff correctly, and I tried to search for possible duplicates but it's difficult to find anything我觉得我正在正确地执行 async 和 await 操作,并且我尝试搜索可能的重复项,但很难找到任何内容

I wonder how TypeScript hasn't caught this, but the problem is that your function returns a Promise<void> instead of a Promise<boolean> .我想知道 TypeScript 怎么没有发现这个问题,但问题是你的函数返回一个Promise<void>而不是Promise<boolean> You never return anything from isLatestVersion , so it ends up with undefined , and your inverted condition always runs the if block.您永远不会从isLatestVersion return任何isLatestVersion ,因此它以undefined结束,并且您的反转条件始终运行if块。

The underlying issue is with await ing a .then(…) chain - you want to use either then and return :潜在的问题是await一个.then(…)- 你想使用thenreturn

const isLatestVersion = (): Promise<boolean> => {
//                     ^ no async
  return VersionCheck.needUpdate({
//^^^^^^
    packageName: 'com.greenshield.mobileclaims',
  }).then((res) => {
    console.log(res)
    return res.isNeeded
  })
}

or only await :或只await

const isLatestVersion = async (): Promise<boolean> => {
  const res = await VersionCheck.needUpdate({
//            ^^^^^
    packageName: 'com.greenshield.mobileclaims',
  });
//  ^ no .then()
  console.log(res)
  return res.isNeeded
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM