Skip to content

Instantly share code, notes, and snippets.

View sabesansathananthan's full-sized avatar
:octocat:
Work

Sathananthan Sabesan sabesansathananthan

:octocat:
Work
View GitHub Profile
@sabesansathananthan
sabesansathananthan / FetchJson.js
Last active January 14, 2021 19:37
Axios Vs Fetch
fetch('url')
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.log(error));
axios({
url: "http://api.com",
method: "POST",
header: {
"Content-Type": "application/json",
},
data: { name: "Sabesan", age: 25 },
});
axios.get(url)
.then((response) => console.log(response))
.catch((error) => console.log(error));
@sabesansathananthan
sabesansathananthan / FetchOverview.js
Last active January 11, 2021 17:19
Axios Vs Fetch
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.catch((error) => console.log(error));
fetch(url)
.then((res) => {
// handle response
})
.catch((error) => {
// handle error
});
@sabesansathananthan
sabesansathananthan / RequestCancel.js
Created January 5, 2021 11:14
Fetch API Tutorial
let controller = new AbortController();
setTimeout(() => controller.abort(), 1000);
try {
let response = await fetch('/long-operation', {
signal: controller.signal
});
} catch(err) {
if (err.name == 'AbortError') {
console.log('Aborted!');
@sabesansathananthan
sabesansathananthan / AbortController.js
Created January 5, 2021 10:59
Fetch API Tutorial
let controller = new AbortController();
let signal = controller.signal;
fetch(url, {
signal: controller.signal
});
signal.addEventListener('abort',
() => console.log('abort!')
);
@sabesansathananthan
sabesansathananthan / Referrer.js
Created January 5, 2021 10:11
Fetch API Tutorial
fetch('/page', {
referrer: ''
});
@sabesansathananthan
sabesansathananthan / Integrity.js
Created January 5, 2021 10:07
Fetch API Tutorial
fetch('http://site.com/file', {
integrity: 'sha256-abcdef'
});
@sabesansathananthan
sabesansathananthan / Keepalive.js
Created January 4, 2021 22:56
Fetch API Tutorial
window.onunload = function() {
fetch('/analytics', {
method: 'POST',
body: "statistics",
keepalive: true
});
};