Created
June 30, 2020 19:59
-
-
Save serdarcevher/578c603269abfae3821ad29de82500e2 to your computer and use it in GitHub Desktop.
Catching errors while fetching a resource with an async function
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
<script> | |
function getData() { | |
return new Promise(function(resolve, reject) { | |
fetch('https://jsonplaceholder.atypicode.com/todos/1') //Var olmayan bir adres | |
.then(response => response.json()) | |
.then(json => { | |
console.log('İkinci mesaj: veriyi aldık'); | |
resolve(json); | |
}).catch(error => { | |
console.log('bir hata oluştu.'); | |
reject(error); | |
}) | |
}); | |
} | |
async function printMessages() { | |
console.log('Birinci mesaj'); | |
let data = await getData(); | |
console.log('Üçüncü mesaj'); | |
console.log(data); | |
console.log(data.title); | |
} | |
printMessages(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment