【问题标题】:Function does not return value when use promise, JavaScript使用promise,JavaScript时函数不返回值
【发布时间】:2022-01-23 10:49:03
【问题描述】:

我对 JavaScript 知之甚少,所以提前对这个问题表示歉意。

我有一个方法:

function userAgent() {
  var result = "";

  navigator.userAgentData
    .getHighEntropyValues(["platformVersion"])
    .then((ua) => {
      if (navigator.userAgentData.platform === "Windows") {
        const majorPlatformVersion = parseInt(ua.platformVersion.split(".")[0]);
        if (majorPlatformVersion >= 13) {
          console.log("Windows 11 or later");
          result = "Windows 11 or later";
        } else if (majorPlatformVersion > 0) {
          console.log("Windows 10");
          result = "Windows 10";
        } else {
          console.log("Before Windows 10");
          result = "Before Windows 10";
        }
      } else {
        console.log("Not running on Windows");
        result = "Not running on Windows";
      }
    });

  return result;
}

它返回空字符串,但打印以控制正确的值。

请告诉我我的错误是什么以及如何在这里返回值,我想以后使用它。

谢谢!

【问题讨论】:

  • 将每个result = 替换为return,并使用某种承诺解析器调用此函数。
  • @bbbbbbbbb 那是问题所在,返回不起作用。
  • @bbbbbbbbb '使用某种承诺解析器' 这是什么?
  • .getHighEntropyValues 是异步的。由于您在非异步函数中使用它,因此它基本上会在返回后的某个时间发生。
  • 超级超级经典的问题,一天被问好多次。我已将其标记为 stackoverflow.com/questions/14220321/… 的重复项

标签: javascript promise user-agent


【解决方案1】:

你需要做两件事。首先返回承诺:

return navigator.userAgentData.getHighEntropyValues(["platformVersion"]).then(...).

然后,return 来自 .then() 处理程序中的所需值。上面的第一个 return 将从你的函数返回 promise。 .then() 处理程序中的 return 将使该值成为您返回的承诺的解析值。

function userAgent() {
    return navigator.userAgentData.getHighEntropyValues(["platformVersion"]).then(ua => {
        if (navigator.userAgentData.platform === "Windows") {
            const majorPlatformVersion = parseInt(ua.platformVersion.split('.')[0]);
            if (majorPlatformVersion >= 13) {
                console.log("Windows 11 or later");
                return "Windows 11 or later";
            } else if (majorPlatformVersion > 0) {
                console.log("Windows 10");
                return "Windows 10";
            } else {
                console.log("Before Windows 10");
                return "Before Windows 10";
            }
        } else {
            console.log("Not running on Windows");
            return "Not running on Windows";
        }
    });
}

那么,你可以这样称呼它:

userAgent().then(result => {
   // use the result here
   console.log(result);
}); 

【讨论】:

  • 它返回 [object%20Promise]
  • 是的,通过链接 .then()const result = await userAgent(); 来解决 Promise。我已将问题标记为 stackoverflow.com/questions/14220321/… 的重复项
  • @Sreawqy - 是的,它返回一个承诺。调用者必须使用.then()await 从promise 中获取解析值。这就是在 Javascript 中使用 Promise 进行异步编程的方式。我建议您阅读有关如何使用 Promise 进行编程的基本知识,因为这是使用 Promise 的一个相当基本的部分,在尝试使用它们之前学习基础知识会很好。
  • 我仍然收到 [object%20Promise],即使我这样调用: const res = userAgent().then();请问可以更新代码吗?很抱歉
  • @Sreawqy - 你不能。该值是异步获取的。因此,它不能直接返回,因为您的函数在值可用之前返回。因此,它必须通过事件、回调或承诺将其传达回调用者,并且调用者必须使用适当的机制来检索值。有关该主题的更多信息,请参阅:How to return the response from an asynchronous call
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
  • 2017-08-01
  • 2019-12-14
  • 2019-10-23
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多