Member-only story
RxJS: Avoiding Unbound Methods
3 min readJun 30, 2018
I often see code that looks a little like this:
Which seems fine.
Well, it is fine — as long as the extractSomeProperty
and handleError
methods do not depend upon the this
context in their implementations.
Why might this be a bad thing?
When unbound methods are passed to RxJS, they will be invoked with an unexpected context for this
. If the method implementations don’t use this
, they will behave as you would expect.
However, there are a number of reasons why, as a general rule, you might want to avoid passing unbound methods:
- If you are in the habit of passing unbound methods, it’s a near certainty that you will, eventually, introduce a bug by passing, as unbound, a method that uses
this
in its implementation. Maybe you’ll have a test that picks it up; maybe you won’t. - Passing unbound methods is something that’s often done with error handlers. And the unit testing of code paths for errors is often neglected.
- The callbacks passed to…