Anil Bhargav
Anil Bhargav

Reputation: 13

Using a return value from a FileReader's onload function

I'm using a function in a loop of files to validate each file, I need to go through the lines and match the version.

I've tried using an observable around my whole operation, unfortunately was not able to get it working the right way.

Any help is appreciated, thanks.

checkDumpVersion(file: File, vcVersion: string): boolean {

  let reader = new FileReader();
  reader.readAsText(file);

  reader.onload = function(e) {
    let firstLine = reader.result.toString().split(/\r\n|\n/).shift();
    let tokens = firstLine.split(/(\s+)/).filter(e => e.trim().length > 0)
    let version = tokens[tokens.indexOf('VERSION') + 1];

    if (version !== VC_TO_DUMP[vcVersion]) {
      console.log(STATUS['versionValidationError']);
      // need to return false here.
    }
  }
  return true;
}

Upvotes: 1

Views: 934

Answers (1)

Faraz Javed
Faraz Javed

Reputation: 154

You can use a callback function to return your response

checkDumpVersion(file: File, vcVersion: string, callback): boolean{

let reader = new FileReader(); reader.readAsText(file);

reader.onload = function(e){
  let firstLine = reader.result.toString().split(/\r\n|\n/).shift();
  let tokens = firstLine.split(/(\s+)/).filter( e => e.trim().length > 0)
  let version = tokens[tokens.indexOf('VERSION') + 1];

  if(version !== VC_TO_DUMP[vcVersion]){
    console.log(STATUS['versionValidationError']);
    // need to return false here.
    callback(false)
  }
}
return true;
}

checkDumpVersion(file, vcVersion, (data)=> {
  console.log(data)
})

Upvotes: 2

Related Questions