How do I return height from function getImgSize(imgSrc)? Bear in mind that onload() is async.
function getImgSize(imgSrc) {
const img = new Image();
img.onload = function() {
const height = img.height;
}
img.src = url;
}
How do I return height from function getImgSize(imgSrc)? Bear in mind that onload() is async.
function getImgSize(imgSrc) {
const img = new Image();
img.onload = function() {
const height = img.height;
}
img.src = url;
}
Copyright © 2021 Jogjafile Inc.
You can wrap it in a promise (called "promisification"):
But then you can only call this function in an async context. Most modern browsers support top-level await (in scripts where type=module), but just in case, you may want to wrap it in a function: