Fetch w/ async/await

PHOTO EMBED

Wed Jun 22 2022 15:30:47 GMT+0000 (Coordinated Universal Time)

Saved by @zaccamp #javascript

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
content_copyCOPY

https://openjavascript.info/2022/01/03/using-fetch-to-make-get-post-put-and-delete-requests/