Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
Function does not return value when use promise, JavaScript

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!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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);
}); 
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!