const getContent = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
// reponse is not correct
if (!response.ok) throw new Error(`We received an invalid response ${await response.text()}`);
const reponseJson = await response.json();
console.log(reponseJson)
}
// async in front of the anonymous function is not needed here because we don't do anything with the promise
// could be getContent().catch(console.log) to catch a potential reject of the promise
document.querySelector('body').addEventListener('click', async event => {
getContent();
});
If we want to keep the async event => {}
document.querySelector('body').addEventListener('click', async event => {
const response = await getContent().catch(console.log);
console.log(response);
});