0

I can't see what I'm doing wrong...push not working and returns nothing

import {nearbyUsers, getLatitude, getLongitude} from './helper'
const users = []
nearbyUsers(session, getLatitude(), getLongitude()).then(res => {
    users.push(res)
console.log(users.lenght) //this works fine
})

console.log(users.lenght) //this prints 0
  • `console.log(users.lenght)` will never print anything but `undefined` – Bravo Jul 19 '21 at 05:08
  • It's `length` and not `lenght`. Anyway, the issue is that the second console.log returns 0 because the callback hasn't executed yet. Your code is asynchronous. – CherryDT Jul 19 '21 at 05:57

1 Answers1

-3

The problem is that the then callback happens asynchronously, meaning that there may (and probably will) be a delay between when the code inside the then callback is executed and the rest of the code outside of it.

If you want to depend on an async function, be sure to read about async/await. The await keyword, when put before an async function, will halt the program until the promise is resolved.

For more see here and here