I've made a function in JS, when i call the function directly it gives the right results but when i put it in a variable it returns undefined. Could someone tell me what i'm doing wrong?
function getGebruikersnaam() {
var test = "replace me";
fetch("restservices/gegevens", {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + window.sessionStorage.getItem("myJWT")
}
}).then(response => response.json()).then(function(myJson) {
console.log(myJson.gebruikersnaam);
test = myJson.gebruikersnaam;
return test;
});
}
getGebruikersnaam();
var name = getGebruikersnaam();
console.log("variable name: " + name);