Skip to content

Instantly share code, notes, and snippets.

@giridharmb
Forked from anil-pace/fetch.js
Created February 26, 2022 03:53
Show Gist options
  • Select an option

  • Save giridharmb/97c88d028d2c1c4c903eb91ff91ac4c9 to your computer and use it in GitHub Desktop.

Select an option

Save giridharmb/97c88d028d2c1c4c903eb91ff91ac4c9 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="myBtn">fetch it</button>
<script>
/* using fetch(promises) */
document.getElementById("myBtn").onclick = () => {
fetch(url)
.then((data) => {
return data.json();
}).then((res) => {
console.log(res);
}).catch(err=>{
// handle any kind of error
console.log(err);
})
};
/*
document.getElementById("myBtn").onclick = async () => {
var data = await fetch(url);
var result = await data.json();
console.log(result);
};
*/
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment