0 points

JavaScript Fetch API - Async Await - Ajax


dashboard

Wed Oct 28 2020 08:31:32 GMT+0000 (UTC)

Posted by @julien #javascript

// Get wrapper
const get = async (url, callback) => {   
  const response = await fetch(url)   
  const status = await response.status      
  if (callback) callback[status](await response.json()) 
}  
// Get usage based callback
get(url, {   
  200: body => doSomething,   
  401: body => showSignInModal,   
  403: body => redirectToSafeArea,   
  422: body => showModalWithInstruction 
})
content_copy Copy

https://medium.com/javascript-in-plain-english/the-ultimate-javascript-fetch-api-cheatsheet-e60b98c8cdbe