Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
javascript return values from .onload method from image object

I am calling the getFileDimensions in another file. I need this method to return values that are inside .onload() method. The values are right I need just to return them using the outer function.

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 }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try using Promises

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;
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!