I am fairly new to javascript, and I have read many articles and similar questions in the forum about promises and fetch, but I could't find a solution in returning a value from fetch. I am trying to read a value from a JSON file by using javascript. For that reason, I made this function to read the value:
function getlastname(){
    
    var myname;
    return fetch('names.json')
        .then((response) => { 
            return response.json().then((data) => {
                myname = data[2].lastname;
                
                return myname;
            }).catch((err) => {
            
            })
        });
}
When I call this function with this code:
var lname= "";
getlastname().then((result) => {
        lname = result;
        console.log(lname);
      })
The variable lname has the right value, but when I am trying to use lname outside .then, it's value is undefined.
Could someone please help me as I don't know how to keep the value of the variable and use it later in the program. Thank you very much in advance.
