Skip to content

Instantly share code, notes, and snippets.

@serdarcevher
Created June 30, 2020 19:59
Show Gist options
  • Save serdarcevher/578c603269abfae3821ad29de82500e2 to your computer and use it in GitHub Desktop.
Save serdarcevher/578c603269abfae3821ad29de82500e2 to your computer and use it in GitHub Desktop.
Catching errors while fetching a resource with an async function
<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