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

156
Views
Facing problem when return an array from a function in JavaScript

I am creating a function to fetch the latitude and longitude using navigator object and returning these two values using an array. The problem is that when I am trying to access one of them using indexing eg:array[0] it's printing undefined and when I am accessing it in console directly everything works fine. Even if I print the whole array in code not in the console directly it also works fine. I am adding a photo for the reference in case I am unable to explain the problem. Any help will be greatly appreciatedphoto for more clear understanding statement no.13 printing undefined

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

async function getLocation() {
        return new Promise(resolve => {
            var array=new Array();
            if(navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(position => {
                    array.push(position.coords.latitude);
                    array.push(position.coords.longitude);
                    resolve(array);
                });
            }
            else {
                resolve(false);
            }
        });
    }

Then just await it, like this:

let user_location=await getLocation();

Or not:

getLocation().then(result =>
        console.log(result)
        );
about 4 years ago · Juan Pablo Isaza 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!