loadcsv(file: File): string[]  {
        let csvRecordsArray: string[];
        this.parse(file).then((p) => {console.log(p); csvRecordsArray = p as string[]; } );
        console.log('mydata', csvRecordsArray);
        return csvRecordsArray;

Console.log inside the "then" prints the data I need. Nothing wrong with the promise. However since it does not block my second console.log has undefined in csvRecordsArray. So I readup up on Promises and learn that I need to await. As soon as I type async to make loadcsv async ts lint says Type 'string[]' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

Please help me get out of this tailspin..

If your function is an async function because you need to use await, those functions always return a Promise.

So the correct signature for this is:

loadcsv(file: File): Promise<string[]>  {
}

You can change the implementation of your loadcsv function by making it async. For that, you'll have to await the function call to this.parse.

Change your implementation like this:

async loadcsv(file: File): Promise<string[]> {
  let csvRecordsArray: string[];
  csvRecordsArray = await this.parse(file);
  console.log('mydata', csvRecordsArray);
  return csvRecordsArray as string[];
}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.