async function myFetch() { try { /* Request */ let res = await fetch('https://reqres.in/api/users/2'); /* Check response */ let resChecked; if (res.ok) { /* If successful: */ console.log("Request successful"); resChecked = res; let data = await resChecked.json(); console.log(data); } else { /* If unsuccessful: */ console.log("Request unsuccessful"); } } catch (err) { console.log(err); } } myFetch() // Prints data object containing information about user 2
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter