I see a common pattern in my application where functions return Observables that do not need to provide a next() value (i.e. save()) but may be part of an Observable chain.
In this working example (link below), I have a demo BookService with save() and getBooks(). The code example save 2 books, get books from service and console.log the books via a single observable chain.
I have tried these two options:
Option 1:
save(title: string): Observable<never>returnsEMPTY. This means subscriber'snext()will never be called. To let the observable chain continue with a single value, Ipipe(defaultIfEmpty(true))after callingsave().Pro: Function works and just call
complete().save()does not need to return a value with no meaning.Con: Calling
defaultIfEmpty(true)to chain observable does not look intuitive.
Option 2:
save(title: string): Observable<boolean>returns a valueof(true). Function works, where bothnext()andcomplete()will be called once.Pro: No need
defaultIfEmpty(true)to chain observable.Con:
next()is always called. Is this considered a con?
Option 3:
- Please let me know if there's a better way.
The code example provides both option 1 and option 2 (commented).
Question: What should functions like save() return? Are there written documentation/article on observables design for such scenario?
Appreciate your thoughts on this.