I have a fetch API that fetches a response from the backend and I have a function inside the fetch that alerts the response. What I am trying to do is run the function on click of a button but the test function is not on the global scope so my button can't access it to run it. Is there any way I can run the function to alert the response on button click after getting the response? Any help is appreciated. Thanks in advance.
fetch(`http://localhost:PORT/api/ROUTE`)
.then(res => res.json())
.then((response) => {
function test() {
alert(response)
}
})
<button onclick="test()">CLICK ME</button>