Advertisement
Gourmet

Using async/await

Jul 15th, 2020
1,510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const addImage = (myBlob) => {
  2.     let objectURL = URL.createObjectURL(myBlob);
  3.     let image = document.createElement('img');
  4.     image.src = objectURL;
  5.     document.body.appendChild(image);
  6. }
  7.  
  8. async function myFetch() {
  9.     let response = await fetch('http://localhost:8081/coffee.jpg');
  10.  
  11.     if(!response.ok) {
  12.         throw new Error(`HTTP error! status: ${response.status}`);
  13.     } else {
  14.         return await response.blob();
  15.  
  16.     }
  17. }
  18.  
  19. myFetch()
  20. .then(addImage)
  21. .catch(e => {
  22.     console.log('Ocorreu um problema com sua operação de busca: ' + e.message);
  23. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement