How to wait for multiple observable streams to resolve and return them together

Nate May

In the observable pipeline below colWithIds$ gives me an observable of a firestore collection (a single array of objects) which I'll refer to as the parent documents. For each of those documents I use flatMap to make a single call for an associated document, which I'll call the child documents. I then store the parent and child docs on local variables:

this.db.colWithIds$('parentCollection')
  .pipe(
    tap(parentDocs => this.parentDocs = _.keyBy(parentDocs, '_id')),
    flatMap(parentDocs => combineLatest(parentDocs.map(doc => this.db.doc(`childCollection/${doc.childId}`).get()))),
    map(snaps => convertSnaps(snaps)),
    tap(childDocs => this.childDocs = _.keyBy(childDocs, '_id'))
  ).subscribe()

The problems with this approach:

  1. There is a moment when the parent docs are available, but the child docs are not
  2. I would like to put this in a service for reuse, so assigning local variables in this way is problematic.

I'm looking for a way that I can wait until all values have been resolved and return a single object in the following form:

{
  parentDocs: [{}, {}, ...], // the documents
  childDocs: [{}, {}, ...], // the documents
}

I'm open to other solutions to the problems listed above. Thanks in advance.

D Pro

Is this what you are looking for? This way you don't query the database twice for parents.

this.db.colWithIds$(`parentCollection`).pipe(
  map(parentDocs => _.keyBy(parentDocs, '_id')),
  flatMap(parentDocs => combineLatest(_.map(parentDocs, parent => this.db.docWithId$(`childDocs/${parent.childId}`))).pipe(
    map(childDocs => _.keyBy(childDocs, '_id')),
    map(childDocs => ({parentDocs, childDocs}))
  ))
);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to add multiple lists together, while keeping them as elements in Python?

分類Dev

cloudflare SSH and sockets: how to run them together?

分類Dev

How to resolve a promise multiple times?

分類Dev

How to bind multiple elements to scroll together?

分類Dev

how to return observable from function that has a promise

分類Dev

How to return stubbed Observable<void> in RxJS

分類Dev

How to return multiple values

分類Dev

Typescript Resolve Observable toEntity

分類Dev

How use multiple observable's results in subscriber

分類Dev

How to merge parallel processing Observables while grouping items from the same Observable together

分類Dev

Three.js - How to group together multiple imported models

分類Dev

How to pass multiple values of type NSString together for a query?

分類Dev

How to lookup multiple items add their corresponding values together and generate a percentage

分類Dev

how to make code wait for function return in node js

分類Dev

How to upload images to firebase storage and wait for URLs to return before continuing

分類Dev

How to populate the store and sequentially await return using Redux Observable?

分類Dev

How can I return an observable with a value that's in a callback?

分類Dev

RTC workflow with multiple streams?

分類Dev

Angular 6 return Observable

分類Dev

Finding numbers in a string and adding them together

分類Dev

How to wait for multiple threads to finish without blocking ui in android?

分類Dev

Javascript how to split string by multiple delimiters and include them in the result

分類Dev

How to use multiple ColumnDataSource and update all of them at once?

分類Dev

Delphi - How to add multiple JPG images to a TCanvas and print them

分類Dev

for loop prints multiple dictionaries how to combine them into one dictionary

分類Dev

How to create multiple labels without them all disappearing at once?

分類Dev

How many temporary objects are created when two objects are added together without the return value optimization?

分類Dev

Rxjs observable wait until some condition is met

分類Dev

MDX-How to return multiple measures

Related 関連記事

  1. 1

    How to add multiple lists together, while keeping them as elements in Python?

  2. 2

    cloudflare SSH and sockets: how to run them together?

  3. 3

    How to resolve a promise multiple times?

  4. 4

    How to bind multiple elements to scroll together?

  5. 5

    how to return observable from function that has a promise

  6. 6

    How to return stubbed Observable<void> in RxJS

  7. 7

    How to return multiple values

  8. 8

    Typescript Resolve Observable toEntity

  9. 9

    How use multiple observable's results in subscriber

  10. 10

    How to merge parallel processing Observables while grouping items from the same Observable together

  11. 11

    Three.js - How to group together multiple imported models

  12. 12

    How to pass multiple values of type NSString together for a query?

  13. 13

    How to lookup multiple items add their corresponding values together and generate a percentage

  14. 14

    how to make code wait for function return in node js

  15. 15

    How to upload images to firebase storage and wait for URLs to return before continuing

  16. 16

    How to populate the store and sequentially await return using Redux Observable?

  17. 17

    How can I return an observable with a value that's in a callback?

  18. 18

    RTC workflow with multiple streams?

  19. 19

    Angular 6 return Observable

  20. 20

    Finding numbers in a string and adding them together

  21. 21

    How to wait for multiple threads to finish without blocking ui in android?

  22. 22

    Javascript how to split string by multiple delimiters and include them in the result

  23. 23

    How to use multiple ColumnDataSource and update all of them at once?

  24. 24

    Delphi - How to add multiple JPG images to a TCanvas and print them

  25. 25

    for loop prints multiple dictionaries how to combine them into one dictionary

  26. 26

    How to create multiple labels without them all disappearing at once?

  27. 27

    How many temporary objects are created when two objects are added together without the return value optimization?

  28. 28

    Rxjs observable wait until some condition is met

  29. 29

    MDX-How to return multiple measures

ホットタグ

アーカイブ