Fetch/promise functions not returning correctly

339 views Asked by At

Im using 2 libraries (heic2any and image conversion) to try convert and compress heic images to jpeg. At the moment Im having issues to get the promises to return in the correct order

e = screenInput(e);

async function screenInput(filename) {
  //checks file extension
  var ext = getExtension(filename);
  switch (ext.toLowerCase()) {
    case 'jpg':
    case 'jpeg':
    case 'png':
    case 'bmp':
      return(compressim(filename));
      break;
    case 'heic':
      var url = URL.createObjectURL(filename)
      var result = await heicConvert(url)
      return compressOnly(result);
      break;
    default:
      return filename
      break;
  }
}


 function heicConvert(input) {
   return fetch(input)
    .then((res)=>res.blob())
    .then((blob)=>(heic2any({blob,toType:"image/jpeg"})))
  }

  function compressOnly(image) {
  var file
  file = new File([imageConversion.compressAccurately(image,500)], image.name, {type: "image/jpeg"})
  return file
}

at the moment its returning a pending promise on

var result = await heicConvert(url)

before even getting to compressOnly(result);

Any help would be appreciated, been stuck on this for a while!

0

There are 0 answers