K
K
Konstantin2020-10-05 18:28:01
Angular
Konstantin, 2020-10-05 18:28:01

How to return an observer?

I have a function in a service to add a document:

addDocument(data: ExistingRelationShips) {
           return this.applicationExistingRelationShipsService
               .create(data)
                   .pipe(tap((document) =>  this.documents.next(this.documents.getValue().concat(document))))
             
    }


I want to call after receiving the result from the server
this.documents.next(this.documents.getValue().concat(document))


then inject the observer with the data to subscribe from outside:
this.service.addDocument().subscribe((res) => console.log(res));


But something got confused

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-10-05
@Junart1

fetchDocuments(data) {
  return this.documents.pipe(
    switchMap(documents => forkJoin([
       of(documents),
       this.applicationExistingRelationShipsService.create(data)
    ])),
    map(([state, update]) => [...state, ...update]),
    tap(state =>  this.documents.next(state)),
  )
}

in general, such a thing does not guarantee the correct order of adding documents, but that's enough for today :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question