I'm trying to store the response from a fetch API call in a global variable. However, the variable which I store the result in returns undefined.
I've tried to use async/await to resolve this issue, but it doesn't seem to have help the situation. I appear to get to a state where a pending promise is returned, but that isn't the desired result.
var obj;
async function getEmails() {
let url = "https://api2.frontapp.com/inboxes/xxxxxx/conversations?limit=50";
return fetch(url, {
body: undefined,
method: 'GET',
headers: {
'Host': 'api2.frontapp.com',
'Authorization': 'Bearer xxxxxx',
"Accept": "application/json",
}
})
.then(res => res.json())
.then(response => {
obj = response;
})
}
getEmails();
console.log(obj);
I expected obj to return the JSON data of the fetch, but it instead returns undefined.