How to get the string returned by fetch directly

I want to use fetch to request a string from the background api. Is there any way to directly take the string out and assign it to the variable?
my original way of writing is this:

var text = "";
fetch(`http://127.0.0.1:8080/`,{
        method: "GET"
    }).then(res => res.text()).then(
      data => {
            text = data
        }
    )
    

after reading many tutorials online, I have converted the data into strings with .text (). It seems that there is still a problem with writing this. How can I take the results out and assign values to external variables? Please give me your advice.
I want to pass the value to the external text variable.


there is no problem with writing this way, but because ajax is asynchronous, you can't get text directly in the code behind fetch

.
Menu