Skip to content

Instantly share code, notes, and snippets.

@SastraNababan
Last active May 28, 2019 16:40
Show Gist options
  • Save SastraNababan/cb19c5dd2f030b6a90a3266a2a855cb3 to your computer and use it in GitHub Desktop.
Save SastraNababan/cb19c5dd2f030b6a90a3266a2a855cb3 to your computer and use it in GitHub Desktop.
Fetch with Promise and Async/await
/* 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