I am making a fetch
request that returns the usual promise. How can I get the result to be saved to a variable that I can use outside the fetch
function?
This doesn't work:
let test
fetch("[request URI]", requestOptions)
.then(response => response.text())
.then(result => { test = result } )
.catch(error => console.log('error', error));
console.log(test); // undefined
Edit:
let test
fetch("[request URI]", requestOptions)
.then(response => response.text())
.then(result => { console.log(result) } ) // here
.catch(error => console.log('error', error));
works and prints the result to the console. So I am aware of how to process the result of an asynchronous request, but having trouble updating a global variable.