Promise chaining vs Async wait
by Kabir Hossain
JavaScript
//Promise chaining:
function logFetch(url) {
return fetch(url)
.then(response => response.text())
.then(text => {
console.log(text);
}).catch(err => {
console.error('fetch failed', err);
});
}
console.log(logFetch('www.google.com'));
//Async function:
async function logFetch2(url) {
try {
const response = await fetch(url);
console.log(await response.text());
}
catch (err) {
console.log('fetch failed', err);
}
}
console.log(logFetch2('www.google.com'));