This file contains hidden or 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
const controller = new AbortController(); | |
const signal = controller.signal; | |
const options = { | |
method: 'POST', | |
signal: signal, | |
body: JSON.stringify({ | |
firstName: 'Sabesan', | |
lastName: 'Sathananthan' | |
}) | |
}; |
This file contains hidden or 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
// request interceptors | |
axios.interceptors.request.use((config)=>{ | |
console.log('Request was sent'); | |
return config; | |
}) | |
// response interceptors | |
axios.interceptors.response.use((response) => { | |
// do an operation on response | |
return response; |
This file contains hidden or 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
fetch = (originalFetch => { | |
return (...arguments) => { | |
const result = originalFetch.apply(this, arguments); | |
return result.then(console.log('Request was sent')); | |
}; | |
})(fetch); | |
fetch('url') | |
.then(response => response.json()) | |
.then(data => { |
This file contains hidden or 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
const config = { | |
onUploadProgress: event => console.log(event.loaded) | |
}; | |
axios.put("/api", data, config); |
This file contains hidden or 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
loadProgressBar(); | |
function downloadFile(url) { | |
axios.get(url, {responseType: 'blob'}) | |
.then(response => { | |
const reader = new window.FileReader(); | |
reader.readAsDataURL(response.data); | |
reader.onload = () => { | |
document.getElementById('img').setAttribute('src', reader.result); | |
} |
This file contains hidden or 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
// original code: https://github.com/AnthumChris/fetch-progress-indicators | |
const element = document.getElementById('progress'); | |
fetch('url') | |
.then(response => { | |
if (!response.ok) { | |
throw Error(response.status+' '+response.statusText) | |
} |
This file contains hidden or 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
axios.get('url') | |
.then((response)=> console.log(response)) | |
.catch((error)=>{ | |
if(error.response){ | |
// When response status code is out of 2xxx range | |
console.log(error.response.data); | |
console.log(error.response.status); | |
console.log(error.response.headers); | |
} else if (error.request){ | |
//When no response was received after request was made |
This file contains hidden or 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
const checkError = response => { | |
if (!response.ok) throw Error(response.statusText); | |
return response.json(); | |
}; | |
fetch("url") | |
.then(checkError) | |
.then(data => console.log(data)) | |
.catch(error => console.log("error", error)); |
This file contains hidden or 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
fetch('url') | |
.then((response)=>{ | |
if(!response.ok){ | |
throw Error (response.statusText); | |
} | |
return response.json(); | |
}) | |
.then((data)=>console.log(data)) | |
.catch((error)=>console.log(error)) | |
This file contains hidden or 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
axios.get('url') | |
.then((response)=>console.log(response)) | |
.catch((error)=>console.log(error)) |