I am trying to do a basic check to see if an image is retina and has a portrait ratio. I have all that checking functionality working perfectly fine. My issue is that in the final function that is supposed to return the boolean I get from my checks it is returning [object Promise]
. I have no idea why this is not returning my boolean when I resolve the promise.
When I run .then(res => console.log(res))
it outputs my boolean response, but the function getImageMeta()
that returns the promise just returns that [object Promise]
which makes me think the promise isn't actually being resolved.
If I could get some assistance that would be great!
/************************************
Check if image is retina portrait
************************************/
const checkIfPortrait = src => !isRetina(src) ? false : getImageMeta(src).then(res => res);
const isRetina = src => src.indexOf('@2x') !== -1;
const isPortrait = ({ naturalWidth, naturalHeight }) => naturalHeight > naturalWidth ? true : false;
function getImageMeta (src) {
const img = new Image();
img.src = src;
return new Promise((resolve, reject) => {
return img.addEventListener("load", function () {
return resolve(isPortrait(this));
}, false);
});
}
export { checkIfPortrait }
This is how I am calling the function:
<img src={media[i].image} alt={media[i].alt} data-portrait={checkIfPortrait(media[i].image)} />
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…