1

I am using the FileReader to parse my csv data and get only the headers. I need to check the headers and do further execution based on the return value.

private checkIfValidFile(file){
    var isValidFile = false;

      var fileContent = new FileReader();
      fileContent.readAsText(file);
      fileContent.onload = () => {
        let text = fileContent.result;
        var data = text.split("\n");
        var headers = data[0].split(",");
        //fileHeaders = headers;
        console.log(headers);
        if (headers.indexOf('"File Name"') > -1) {
          isValidFile = true;
        }
      };
    return isValidFile
}
private uploadFiles(files: any) {
for (let index = 0; index < files.length; index++) {
        var isValidFile = this.checkIfValidFile(files[index]);
        if(isValidFile){
          //Execute some lines
        }
      }
}

But the return value gets returned before the onload. How can i do this?

1

2 Answers 2

2

use "Promise" to achive that like this :

private checkIfValidFile(file):Promise<boolean>{

      var fileContent = new FileReader();
      fileContent.readAsText(file);
      
      return new Promise((resolve,reject)=>{
          fileContent.onload = () => {
          let text = fileContent.result;
          var data = text.split("\n");
          var headers = data[0].split(",");
          //fileHeaders = headers;
          console.log(headers);
          if (headers.indexOf('"File Name"') > -1) {
            resolve(true);
          }
          resolve(false);
        };
      });
      
}
private uploadFiles(files: any) {
for (let index = 0; index < files.length; index++) {
        this.checkIfValidFile(files[index]).then((isValidFile)=>{
           if(isValidFile){
             //Execute some lines
           }
        });
        
      }
}

Sign up to request clarification or add additional context in comments.

Comments

1

what about use promise?

like:

 private checkIfValidFile(file): Promise<boolean> {
    return new Promise((resolve, reject) => { // RETURN PROMISE FFORM YOUR FUNC

      try {
        var isValidFile = false;

        var fileContent = new FileReader();
        fileContent.readAsText(file);
        fileContent.onload = () => {
          let text = fileContent.result;
          var data = text.split("\n");
          var headers = data[0].split(",");
          //fileHeaders = headers;
          console.log(headers);
          if (headers.indexOf('"File Name"') > -1) {
            isValidFile = true;
            resolve(isValidFile);
          }
        };
      } catch (error) {
        reject(error);
      }


    });

  }
  private uploadFiles(files: any) {
    for (let index = 0; index < files.length; index++) {
      this.checkIfValidFile(files[index]).then((isValidFile) => {
        if (isValidFile) {
          //Execute some lines
        }
      });

    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.