The following pieces of code are giving me the same result. What is the difference between the use of .then
and async
/await
to fetch data?
// Code 1
function fetchData() {
fetch(url)
.then(response => response.json())
.then(json => console.log(json))
}
// Code 2
async function fetchData() {
const response = await fetch(url);
const json = await response.json();
console.log(json);
}