Answers for "async vs await javascript"

0

async vs await javascript

async function myFetch() {
  let response = await fetch('coffee.jpg');

  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }

  let myBlob = await response.blob();

  let objectURL = URL.createObjectURL(myBlob);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
  }
}

myFetch()
.catch(e => {
  console.log('There has been a problem with your fetch operation: ' + e.message);
});
Posted by: Guest on April-18-2021
0

async vs await javascript

fetch('coffee.jpg')
.then(response => {
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }
  return response.blob();
})
.then(myBlob => {
  let objectURL = URL.createObjectURL(myBlob);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
})
.catch(e => {
  console.log('There has been a problem with your fetch operation: ' + e.message);
});
Posted by: Guest on April-18-2021
0

async await vs then

async function fancyShowRainbow(){
  const response = await fetch("rainbow.jpg");
  const blob = await response.blob();
  document.querySelector(".my-image").src = URL.createObjectURL(blob);
}


function showRainbow(){
  fetch("rainbow.jpg")
  .then(res => {
    console.log(res);
    return res.blob();
  })
  .then(blob => {
    document.querySelector(".my-image").src = URL.createObjectURL(blob);
    console.log(blob);
  })
  .catch(error => {
    console.log("ERROR!!");
    console.error(error);
  });
}
Posted by: Guest on July-19-2021
0

async vs await javascript

async function myFetch() {
  let response = await fetch('coffee.jpg');
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }
  return await response.blob();

}

myFetch().then((blob) => {
  let objectURL = URL.createObjectURL(blob);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
}).catch(e => console.log(e));
Posted by: Guest on April-18-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language