0

I have this API call in an angular application

  async upload(file: FileToUpload) { // this is where the error occurs
    file.id = UUID.UUID();

    this.delegates.fileManager.uploadFile(file).subscribe({
      next: async () => {
        this.files = await (await this.delegates.fileManager.getFilesAsync()).files;
        console.log(this.files);
      }
    });
  }

the function should upload the file and refresh the file listing with an another api call. This aproach works on the delete function

async deleteFile(file: FileToUpload) {
    this.delegates.fileManager.deleteFileById(file.id).subscribe({
      next: async () => {
        this.files = await (await this.delegates.fileManager.getFilesAsync()).files;
      }
    })
  }

and while the uploading works, the list doesn't refresh instantly as I have to refresh the page to see the new item. It throws ERROR SyntaxError: Unexpected token ')' even if the this.files = await (await this.delegates.fileManager.getFilesAsync()).files; is not here.

Danny220
  • 25
  • 8
  • Hi! Do you really need the async way? I mean you can use rxjs operator to update the files after upload. Using mergeMap or switchMap. Example: https://stackoverflow.com/a/52317734/12442825 – Marc Guillem Mar 13 '23 at 13:05
  • now I did `await this.delegates.fileManager.uploadFileAsync(file).finally(async () => { this.files = await (await this.delegates.fileManager.getFilesAsync()).files; });` and works but throws the same error... strange – Danny220 Mar 13 '23 at 13:06
  • with mergeMap it does have the original symptoms... the file gets uploaded but the list stays the same – Danny220 Mar 13 '23 at 13:12

1 Answers1

0

Not sure if I like the mismatch of subscribe and await, I would try either all subscribe or all async/await:

upload(file: FileToUpload) { // this is where the error occurs
    file.id = UUID.UUID();
    this.delegates.fileManager.uploadFile(file).subscribe({
      next:() => {
        this.delegates.fileManager.getFilesAsync().subscribe({
           next: (results) => {
              this.files = results.files;
           } 
        })
      }
    });
  }
Wen W
  • 2,434
  • 1
  • 10
  • 15