Estoy llamando a getFileDimensions en otro archivo. Necesito este método para devolver valores que están dentro del método .onload() . Los valores son correctos, solo necesito devolverlos usando la función externa.
export const getFileDimensions = file => { var reader = new FileReader(); //Read the contents of Image File. reader.readAsDataURL(file); var width, height = null reader.onload = function (e) { //Initiate the JavaScript Image object. var image = new Image(); //Set the Base64 string return from FileReader as source. image.src = e.target.result; //Validate the File Height and Width. image.onload = function () { console.log("Image loaded", this.width, this.height) height = this.height; width = this.width; return { width: this.width, height: this.height } }; return { width: width, height: height } }; return { width: width, height: height } }Intenta usar Promesas
export const getFileDimensions = async (file) => { var reader = new FileReader(); //Read the contents of Image File. reader.readAsDataURL(file); var width, height = null; const result = await new Promise((resolve) => { reader.onload = function (e) { //Initiate the JavaScript Image object. var image = new Image(); //Set the Base64 string return from FileReader as source. image.src = e.target.result; //Validate the File Height and Width. image.onload = function () { console.log("Image loaded", this.width, this.height); height = this.height; width = this.width; resolve({ width: this.width, height: this.height }); }; }; }); return result; };