Skip to content

Instantly share code, notes, and snippets.

@Antoinebr
Created May 28, 2019 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Antoinebr/9eb6851757d1669bd85cf32b03c9e558 to your computer and use it in GitHub Desktop.
Save Antoinebr/9eb6851757d1669bd85cf32b03c9e558 to your computer and use it in GitHub Desktop.
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);

});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment