I start by saying that I am not 100% sure this is the problem, I mean using await and async.
This is the scenario:
I run this when I first load the page, and works fine, I get the data
:
externalContent(url);
function externalContent(url) {
fetch(url)
.then(res => res.json())
.then(data => {
...cool data...
});
}
But then I need to be able to click a button and re run that function with the fetch
So I do
$(".alm-filters--button").on("click", function() {
externalContent(url);
});
But when I click, It gives me an error on .then(res => res.json())
The error says: Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
I believe there is an asynchronous issue, I tried, but I do not know enough about using async and await, yet I tried:
async function externalContent(url) {
await fetch(url)
.then(res => res.json())
.then(data => {
...cool data...
});
}
But et, I get the same error.