Last active
May 28, 2019 16:40
-
-
Save SastraNababan/cb19c5dd2f030b6a90a3266a2a855cb3 to your computer and use it in GitHub Desktop.
Fetch with Promise and Async/await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Promise */ | |
const endpoint ='https://jsonplaceholder.typicode.com/users/' | |
function fetchWithPromise (id) { | |
fetch(endpoint + id) | |
.then(response => { | |
return response.json(); | |
}) | |
.then(user => { | |
console.log(user); | |
}) | |
} | |
/* Async/Await */ | |
async function fetchWithAsyncAwait (id) { | |
let response = await fetch(endpoint + id) | |
response = await response.json() | |
console.log(response) | |
} | |
fetchWithPromise(1) | |
fetchWithAsyncAwait(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment