Search code examples
rxjsrxjs5rxjs6switchmap

Return object inside switchMap


I am new to rjxs programming so wondering how can I do the following.

    const downloadUrls$: any = filelist.map((file: any) => {
      const fileName = ...
      const path = ...
      const fileRef = ....
      const task: any = ...

      // get notified when the download URL is available
      return task.snapshotChanges().pipe(
        filter(snap => snap.state === TaskState.SUCCESS),
        switchMap(() => from(fileRef.getDownloadURL()))
      );
    });

So in the above code instead of doing from(fileRef.getDownloadURL()) is there is a way I can do create a object like below and the return will be a list of below objects.

             from (
                {
                 name: fileName,
                 filepath: fileRef.getDownloadURL(),
                 relativePath: path
                }
              )

Method signature

 `.getDownloadURL(): Observable<any>`  

Solution

  • I don't have anything that is runnable from the code you provided, so I'm writing this just as a concept.

    What you have is something that is already an Observable (fileRef.getDownloadURL()). And what we need is the new object. We can get there by mapping over the Observable from earlier, and changing it to the result that we need. Something along the lines of:

    task.snapshotChanges().pipe(
        filter(snap => snap.state === TaskState.SUCCESS),
        switchMap(() => {
            return fileRef.getDownloadURL().pipe(
                map(url => ({
                    name: fileName,
                    filepath: url,
                    relativePath: path
                }))
            );
        })
    );