1

I have a very few knowledge in JavaScript, so sorry in advance for this question.

I have a method:

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;
}

And it returns empty string, but prints to console the correct value.

Please, tell me what is my mistake and how to return value here, I want to use it after.

Thank you!

MarioG8
  • 5,122
  • 4
  • 13
  • 29
  • 1
    Replace each `result =` with `return`, and call this function using a promise resolver of some sort. –  Dec 22 '21 at 06:50
  • @bbbbbbbbb that is the problem, return doesn't work. –  Dec 22 '21 at 06:51
  • @bbbbbbbbb 'using a promise resolver of some sort' what is it? –  Dec 22 '21 at 06:51
  • 1
    `.getHighEntropyValues` is async. Since your using it in a non-async function, it basically happens some time after the return. – Ouroborus Dec 22 '21 at 06:57
  • @Sreawqy: for example, `.then`... or `await`, in the case of `async` functions. –  Dec 22 '21 at 07:08
  • @Sreawqy, Try to change `result` as an empty array instead of an empty string. Then push that return value to that array. Like this `result.push("Windows 11 or later")` in each if condition. I'm suspecting that each & every time the value gets overwritten. Try it as an array & push that values – Plutus Dec 22 '21 at 07:08
  • @Plutus: It won't work. The `result` is returned outside of the `.then`, and will therefore contain nothing but its initial value (an empty string). Ask yourself what is the point in doing a single push rather than just assigning a value directly. –  Dec 22 '21 at 07:09
  • 1
    Super super classic question, it's being asked multiple times a day. I've marked it as duplicate of https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Jeremy Thille Dec 22 '21 at 07:25
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – FZs Dec 22 '21 at 08:39

1 Answers1

3

You need to do two things. First return the promise with:

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

Then, return the desired value from within the .then() handler. The first return above will return the promise from your function. The return inside the .then() handler will make that value become the resolved value of the promise you're returning.

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";
        }
    });
}

Then, you would call that like this:

userAgent().then(result => {
   // use the result here
   console.log(result);
}); 
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • it returns [object%20Promise] –  Dec 22 '21 at 07:11
  • Yes, resolve the Promise by chaining `.then()`, or with `const result = await userAgent();`. I've marked the question as a duplicate of https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Jeremy Thille Dec 22 '21 at 07:26
  • @Sreawqy - Yes, it returns a promise. The caller must use `.then()` or `await` to get the resolved value from the promise. That's how asynchronous programming with promises works in Javascript. I'd suggest you do some basic reading on how programming with promises works as this is a fairly fundamental part of using promises and it would be good to learn the basics before trying to use them. – jfriend00 Dec 22 '21 at 07:40
  • I still receive [object%20Promise], even when I call like this: const res = userAgent().then(); Can you please, update the code? Sorry about it –  Dec 22 '21 at 07:47
  • @Sreawqy - That's not how you get a value out of a promise. You access the value from INSIDE the `.then()` callback. Please read some basic instructions on how to use promises. It will really help you. Also, see what I added to the end of my answer. – jfriend00 Dec 22 '21 at 07:53
  • I want to return it as a usual value (like string). Don't want to use promise in calling –  Dec 22 '21 at 08:03
  • 1
    @Sreawqy - You can't. The value is obtained asynchronously. Thus, it cannot be returned directly because your function returns BEFORE the value is available. Thus, it must be communicated back to the caller via an event, a callback or a promise and the caller must use that appropriate mechanism to retrieve the value. For more info on that topic see: [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/14220323#14220323). – jfriend00 Dec 22 '21 at 08:08