Home > Mobile >  AngularFire storage listAll build array of promises and forkjoin
AngularFire storage listAll build array of promises and forkjoin

Time:08-01

I'm looking for a hand with creating an array of promises and executing them all with RXJS forkjoin to build a result set.

I'm using AngularFire's storage module to return all the uploaded files in a given location like so:

  getFileList(folderTree: string, folderName: string) {
    const filePath = folderTree   '/'   folderName;
    const fileRef = this.storage.ref(filePath);
    return fileRef.listAll();
  }

Subscribing to the above like so:

    this.filesService
      .getFileList(this.folderTree, this.folderName)
      .subscribe((result) => {
        //do stuff
      });

The result of this is a ListResultCompat object that contains an array called 'items' containing ReferenceCompat objects. Each ReferenceCompat object has a method available called 'getDownloadURL' which returns a promise that resolves with a URL. I want to make these promises into observables and then forkjoin them to create a final array of the download URLs but the ordinary way of converting to observables with RXJS 'of' is not behaving as I'd expect. Clearly I'm missing something.

If I do the following:

    this.filesService
      .getFileList(this.folderTree, this.folderName)
      .subscribe((result) => {
        this.files = result.items.map((file) => of(file.getDownloadURL()));
        //do stuff
      });

The above produces an array of observables as might be expected, however when I forkjoin them, they do not resolve as I'd hoped. Executing this:

    this.filesService
      .getFileList(this.folderTree, this.folderName)
      .subscribe((result) => {
        this.files = result.items.map((file) => of(file.getDownloadURL()));

        forkJoin(this.files).subscribe((data) => {
          //want data to be the resolved promises so I can map out the URLs
        });
      });

Results in an array of ZoneAwarePromise objects which are resolving with a property called '__zone_symbol__value' that contains the URL I'm looking for, however I want the promises to be resolved as part of the forkjoin and just be able to map out the URL value.

Just not sure what step I'm missing here.

CodePudding user response:

If they are Promises, you should use from and not of.

  • Related