I would like to use the returned IP-adress in my normal JavsScript code outside of fetch function. Everything works fine when I print out the IP-adress inside the fetch funtion. But as soon as I try to log "ip" outside of it, it returns "undefined". I know this may be because fetch is asynchronus, but I do not know how to solve this problem. I would really appreciate some help!
function getIP(){
let ip;
fetch('https://api.ipify.org/?format=json')
.then(result => result.json())
.then(data => {
ip = data.ip;
console.log(ip);
});
console.log(ip);
}