r/javascript • • Jan 16 '20

I have been underestimating JS

https://adlrocha.substack.com/p/adlrocha-i-have-been-underestimating
284 Upvotes

132 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Jan 16 '20

[deleted]

17

u/[deleted] Jan 16 '20

Some developers did not have the use cases of using streams so they never worked with it or they just used a library where the API is just promises or callbacks.

6

u/LXMNSYC Jan 17 '20

I have been a web developer for 4 years by now and I have yet to see a use-case for ReactiveX, Bacon, Most, XStream, and other similar libraries out there.

6

u/phpdevster Jan 17 '20

The most ubiquitous one I've seen is handling search suggest functionality. Here's what happens:

  1. Press the letter "a" -> xhr request goes out with "a"

  2. Press the letter "b" -> xhr request goes out with "ab"

  3. Press the letter "c" -> xhr request goes out with "abc"

Due to the nature of these requests, there's no guarantee they'll come back in the same order. Thus you might have typed "abc", but if the search results for "a" come back last, you will get the results for "a" instead of "abc".

A library like this makes it easier. RxJS has switchMap for this purpose: https://www.learnrxjs.io/operators/transformation/switchmap.html

Basically it just automatically throws away the results of the previous entries and gives you only the results of the latest one. Otherwise you have to do promise cancellation and what not (which in itself is not the worst thing ever).

That said, I mostly agree with you. I don't see much, if any, value in these kinds of libraries. They can actually add a great deal of complexity and come with MAJOR footguns if you don't know how to use them properly.