Given the following function:
function check_min_sizing (input, options) {
reader = new FileReader();
reader.onload = function (e) {
img_rsc = e.target.result;
image = new Image();
image.onload = function () {
if (image.width < options.min_width || image.height < options.min_height) {
return false;
} else {
return true;
}
};
image.src = img_rsc;
};
reader.readAsDataURL(input.files[0]);
}
How can I make the whole function return true or false? It doesn’t work as-is above because (I believe) the function finishes executing before the onload’s do…
I want to be able to do something like:
if (check_min_sizing(input, options)) {
// do something successful
} else {
// throw an error
}
There are ways of making this work, by blocking the function until the image has loaded but its a bad thing to do. The calling code should pass in a callback function, or two if you want an error callback a well.