I have a for loop for checking multiple uploaded image's aspect ratio, after completing the loop i want to check ratio in if else condition to redirect user. Problem is the conditions are checked before loop finishes, I need the loop to be completed before checking conditions. I found out async whilst might be suitable here but i'm confused about the best approach for implemetation, can anyone give me workaround to perform the code sequentially.
//check image ratio
var validImageRatio = true;
for(i=0; i<req.files.propertyPhoto.length; i++){
var tempFile = req.files.propertyPhoto[i].tempFilePath.replace(/\\/g, '/');
var ratio;var width;var height;
var acceptedRatio = 3;
//get image ratio
sizeOf(tempFile, function (err, dimensions) {
width = dimensions.width;
height = dimensions.height;
ratio = width/height;
});
if (ratio < (acceptedRatio - 0.1) || ratio > (acceptedRatio + 0.1)) {
validImageRatio = false;
break;
}
}
//if ratio invalid, redirect
if (!validImageRatio) {
...
}
//if ratio valid, upload
else{
...
}