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
Promise.all([ | |
fetch('https://api.github.com/users/iliakan'), | |
fetch('https://api.github.com/users/taylorotwell') | |
]) | |
.then(async([res1, res2]) => { //async function | |
const a = await res1.json(); | |
const b = await res2.json(); | |
console.log(a.login + ' has ' + a.public_repos + ' public repos on GitHub'); | |
console.log(b.login + ' has ' + b.public_repos + ' public repos on GitHub'); | |
}) |
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
axios.all([ | |
axios.get('https://api.github.com/users/iliakan'), | |
axios.get('https://api.github.com/users/taylorotwell') | |
]) | |
.then(axios.spread((obj1, obj2) => { | |
// Ambas requests foram finalizadas | |
console.log(obj1.data.login + ' has ' + obj1.data.public_repos + ' public repos on GitHub'); | |
console.log(obj2.data.login + ' has ' + obj2.data.public_repos + ' public repos on GitHub'); | |
})); |
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
<img id="img"> | |
<script> | |
loadProgressBar(); | |
const url = 'https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg'; | |
function downloadFile(url) { | |
axios.get(url, {responseType: 'blob'}) | |
.then(response => { | |
const reader = new window.FileReader(); | |
reader.readAsDataURL(response.data); | |
reader.onload = () => { |
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
// código original: https://github.com/AnthumChris/fetch-progress-indicators | |
<div id="progress" src="">progress</div> | |
<img id="img"> | |
<script> | |
'use strict' | |
const element = document.getElementById('progress'); | |
fetch('https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg') | |
.then(response => { | |
if (!response.ok) { | |
throw Error(response.status+' '+response.statusText) |
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
fetch = (originalFetch => { | |
return (...arguments) => { | |
const result = originalFetch.apply(this, arguments); | |
return result.then(console.log('A requisição foi enviada')); | |
}; | |
})(fetch); | |
fetch('https://api.github.com/orgs/axios') | |
.then(response => response.json()) | |
.then(data => { | |
console.log(data) |
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
axios.interceptors.request.use(config => { | |
// loga(log) uma mensagem antes da requisição HTTP ser enviada | |
console.log('A requisição foi enviada'); | |
return config; | |
}); | |
// faz uma requisição GET | |
axios.get('https://api.github.com/users/sideshowbarker') | |
.then(response => { | |
console.log(response.data); | |
}); |
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
// axios | |
axios.get('https://api.github.com/orgs/axios') | |
.then(response => { | |
console.log(response.data); | |
}, error => { | |
console.log(error); | |
}); | |
// fetch() | |
fetch('https://api.github.com/orgs/axios') | |
.then(response => response.json()) // passo extra |
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
const controller = new AbortController(); | |
const options = { | |
method: 'POST', | |
signal: controller.signal, | |
body: JSON.stringify({ | |
firstName: 'David', | |
lastName: 'Pollock' | |
}) | |
}; | |
const promise = fetch('/login', options); |
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
axios({ | |
method: 'post', | |
url: '/login', | |
timeout: 4000, // 4 segundos timeout | |
data: { | |
firstName: 'David', | |
lastName: 'Pollock' | |
} | |
}) | |
.then(response => {/* tratamento da resposta */}) |
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
// fetch() | |
const url = 'http://localhost/test.htm'; | |
const options = { | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json;charset=UTF-8' | |
}, | |
body: JSON.stringify({ |
NewerOlder