Quote:
If I use function lsinfo() without async the code works fine but info['_myip'] return null.
The
async
keyword does nothing in your context because you are not using an
await
keyword in the function scope. You say that it returns null, the only plausible reason that I could think of — remember that I cannot access your machine, so debugging is a bit hard — is that your
localStorage.getItem("gioloc");
returns a
null
object. Meaning, the item with key
gioloc
does not exist "yet" in your
localStorage
.
Quote:
when I use function lsinfo() with async then info['_myip'] return data but lsinfo() retuen 0 length
This is very unclear sentence. Async will not change the return type or the value of the return — other than
making it a promise[
^] that can be awaited, if anything. Secondly, your lsinfo is a method that you need to call. Applying the length to the return would provide the length of the return value (which in your method is a
new Object()
with some added attributes). Perhaps, you want to do
lsinfo()['_myip'].length
? Continue reading for a potential fix, I think you are not using the
localStorage
API correctly.
localStorage.gioloc = lat + ',' + lng;
Also, this is not how the
localStorage
API is supposed to be used. Here you are setting a field to the
localStorage
object "itself". To set a key in the
localStorage
you need to use the
setItem
function.
function displayLocationInfo(position) {
const lng = position.coords.longitude;
const lat = position.coords.latitude;
localStorage.setItem("gioloc", lat + ',' + lng);
}
This should set the item and the next time you access it, you will be able to read the value.
Window.localStorage - Web APIs | MDN[
^]