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

238
Views
How do I wait for FileReader onload to complete first

I am working with FileReader and I have come across an issue with the onLoad method of FileReader not being executed synchronously with the rest of the code. Basically I have 2 functions like these:

    imageExists(url, callback) {
      var img = new Image();
      img.onload = function () { callback(true); };
      img.onerror = function () { callback(false); };
      img.src = url;
    }

    isImageCorrupt(file): boolean {
      var reader = new FileReader();
      var isCorrupt = false;
      reader.readAsDataURL(file);
      reader.onload = (e) => {
        this.imageExists(e.target.result, (exists) => {
            if (exists) {
                isCorrupt = false;
                // Image is not corrupt
            } else {
                isCorrupt = true;
                //Image is corrupt
            }
        });
    };

    return isCorrupt;
   }

The isImageCorrupt() function calls the reader.onLoad which calls the imageExists callback function, which also contains a image onload method.

The problem is that during the execution of the isImageCorrupt() function, the reader.onLoad has not changed the value of isCorrupt yet but the function has returned the value in the last line which is always false.

I want my function to wait for the reader.onLoad to finish its execution before the function returns the value.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

maybe something like this?

 isImageCorrupt(file): Promise<Boolean> {
    return new Promise((resolve) => {
        var reader = new FileReader();
        reader.onload = (e) => {
            var img = new Image();
            img.onload = function () {
              resolve(false);
            };
            img.onerror = function () {
              resolve(true);
            };
            img.src = <string>e.target.result;
        }
        reader.readAsDataURL(file);
    });
}

*disclaimer: I did not test it

about 4 years ago · Juan Pablo Isaza Report

0

You could use Promises. The code could be still refactorized using async/await

    isImageCorrupt(file): Promise<boolean> {
      return new Promise<boolean>((resolve,reject)=>{
      var reader = new FileReader();
      
      reader.readAsDataURL(file);
      reader.onload = ()=>{
      var img = new Image();
      img.src = reader.result;
            img.onload = function () {
              resolve(false);
            };
            img.onerror = function () {
              resolve(true);
            };
            
}

reader.onerror=()=>{reject(true)}
        });
    };


   
       isImageCorrupt(yourFile).then((result)=>{/*HERE USE RESULT*/},(error)=>{HERE USE ERROR RESULT})

However you shouldn't return true o false, but resolve if it's ok and reject otherwise, whithout a boolean value in this way

isImageCorrupt(file): Promise<void> {
          return new Promise<void>((resolve,reject)=>{
          var reader = new FileReader();
          
          reader.readAsDataURL(file);
          reader.onload = ()=>{
          var img = new Image();
          img.src = reader.result;
                img.onload = function () {
                  resolve();
                };
                img.onerror = function () {
                  reject();
                };
                
    }
    
    reader.onerror=()=>{reject()}
            });
        };
    
    
       
       

     isImageCorrupt(yourFile).then(()=>{/*HERE OK*/},()=> {HERE 
  THERE WAS SOME ERROR/PROBLEM})
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!