4

After reading a lot on Observables after coming from a large Promise-based application I understand their power for utilizing streams/event patterns, however, I feel like there are times when using Observables feels clunky and overkill.

When you want to fetch some data, especially for paged data, the Observable is perfect. You can wire up an initial size and offset for the paging and have updates to the page and size trigger an update to the observable and it fetches more data, transforms it, etc.

However, when we are doing something simple like a DELETE request to /api/books/123 and there is no valuable response, it feels awkward to use Observables as theres nothing to "observe" and you have to "trigger" the request to be made by subscribing to it.

Heres an example:

Promise

await myService.DeleteBook('123');
// the book is now deleted

Observable

myService.DeleteBook('123')
// the book is still there as the request isn't sent yet
.subscribe(x => {
   // finally in here the book is deleted, but 'x'
   // is pretty much worthless so this method pretty much does nothing.
});

So a few things come to mind:

  1. It feels really awkward to subscribe to a DELETE request
  2. It's extra code for basically no benefit
  3. I like how I can control whether I block on the Promise by simple adding/removing await from the line

All of the bloggers and articles I see around Observables seem to focus on using Observables all the time and to never use Promises.

This guy seemed to be the only person to "stand-up" to Observables and tried to argue why they should only be used when they make sense, but all of the comments are people just bashing him and saying that Observables are still the silver bullet.

Can someone explain to me why there is such a strongly opinionated stance towards using Observables in all cases outright?

1
  • Matt, if you need concatenate Promise you can go crazy easily. The observables can chain, fork, debounce,swichmap, ... (it's only an opinion)
    – Eliseo
    Commented Mar 12, 2019 at 22:10

3 Answers 3

2
myService.DeleteBook('123')
// the book is still there as the request isn't sent yet
.subscribe(x => {
   // finally in here the book is deleted, but 'x'
   // is pretty much worthless so this method pretty much does nothing.
});
  • "x" is not totally worthless. You still want to check the response and show success message accordingly or handle the HTTP error in the error callback. Right? With promises, you will have to wrap it in to try/catch to handle the HTTP error.

  • The other best thing about observables is they can be combined with other observables and chained. If you take a look at RXJS operators, you can do some pretty cool stuff with them. It's the Operators that makes fun to work with observables.

  • For example in your case, imagine there is a requirement to show warning dialog before deleting. You can chain the delete observable with the warning observable along with filter operator to only delete if "Yes" was clicked by the user. This would be easier to accomplish if you are using observables over promises.

  • Also for consistency reason, you want to keep the same subscribe pattern everywhere. Like, you don't want to use Promise in one place and observables in others to confuse other developers.

0

Observables are much more than that you've mentioned. They are not about streaming, not about event-handling, even not about composition, although all those factors do matter. Observable<T> is kind of T's superpower: once you got it, you can't get back. Spiderman could never become just a Peter Parcker. In particular, that means that you "lift" your regular code thus giving it a great ability to do.. all the awesome things rx does.

The best way to understand rx is to consider it as a generalized function - the one, which can do infinitely many return-statements over time. Attempt to get rid of that one kind of near the nonse of getting rid from the.. functions themselves. Rather than trying to get rid of it, you have to try to fit your solution into it in exactly the same way like you define your abstract solution in terms of certain classes equiped by certain functions of certain signatures or even nakedly exported functions. In few cases it requires you to map something that not (yet) observable (like promise) to a semantically equal observable (from(...) does it). So you have to use those mappers freely with no doubts.

Let me know if more detailed explanation is needed.

-1

I would tend to agree with you, especially for the use cases here, and also even for GETs. For Angular I like the .toPromise():

let book = await httpObservableService.GetBook('123').toPromise()
1
  • Thank you! This is exactly what I was thinking. We can make everything observables inside of the service, but the consumer doesn't really need all the extra baggage that comes with Observables. I found googling seems to be one-sided in that discussion though and I'm not sure why Commented Mar 12, 2019 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.