0

I admit that I still do not fully fathom async/promises as you can probably gather from my code but how am I supposed to get my function to wait for checkImageRatio in this circumstance.

async function checkImageRatio(id){
    let myPromise = new Promise(function(resolve) {
        let test = false;
        $(`#image-${id}`).find('img').each(async function(){
            let img = new Image();

            img.src = this.src;
            await img.decode();

            console.log((img.width/img.height));

            if(Math.abs(1 - img.width/img.height) > 0.02){
                $(`#image-${id}-main-image`).css('border', '1px red solid');
                $(`#${id}-warning`).effect('shake', {distance: 8, times: 2}, 200);
                test = true;
            }
        });
        resolve(test); 
    })
}

async function saveProduct(id){
  var imageTest = await checkImageRatio(id).then(result=>{return result});
  console.log(imageTest);
  console.log("here");
}

Why is image test not resolving here??

4D_Liam
  • 81
  • 5
  • You need to return `myPromise` from `checkImageRatio`. A function needs to return a promise in order for it to be `await`'ed. – Cully May 05 '22 at 00:07
  • here: https://www.typescriptlang.org/play?#code/IYZwngdgxgBAZgV2gFwJYHsIygCwKZQDWAkgLbADmeASsGugBSoAmAXDBAqQEZ4BOASgDeAKBjiYfPMgR8sEPAHcYABT7pSqEHgaIUGCAykh0AGwBueATFES7MU9JjI8IZDAC8MAETIcdAHIQGFI8Z1dkbwBuMXtxYzNLBhc3ASiYWPEAXwERLJERUEhYPSh6LBBgSzV0ZgQypjYOLl5BWxhzYD4YVHIqABUIzxhgRWBUd1wCEj6aOgxGgQA6PzxDYwRTZA8APiEpGTlJV03kHJjxKEwTRyXTdAoGAOIAWQBBAHEAURh+r4BlfqsAIAGl6lDwg1SF2w1zMeDuDwY3jecBc3QSFlQEAo7AAEugYDh0N40nkCpVquo6g0AMwCIA –  May 07 '22 at 13:58

0 Answers0