Skip to content

Instantly share code, notes, and snippets.

@khaosdoctor
Last active September 21, 2017 15:38
Show Gist options
  • Save khaosdoctor/cd58b2520bd18ad82336082765180e65 to your computer and use it in GitHub Desktop.
Save khaosdoctor/cd58b2520bd18ad82336082765180e65 to your computer and use it in GitHub Desktop.
Fetch API example
if (self.fetch) { // Verificamos se existe suporte do navegador ao Fetch
var myImage = document.querySelector('img');
fetch('arquivo.jpg') // Busca um arquivo de uma URL
.then(function(response) { // Trata a resposta em uma promise
return response.blob(); // Busca o blob desta resposta
})
.then(function(myBlob) { // Passa o blob para outra função
var objectURL = URL.createObjectURL(myBlob); // Cria uma URL
myImage.src = objectURL; // Adiciona ao SRC da imagem
});
} else { // Se não houver podemos fazer o XMLHttpRequest }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment