1

In Angular, it appears as though many operations can receive a straight value or an observable for that value. I want to implement a function that does the same. Are there any pitfalls to just implementing a check and casting: if(typeof input.subscribe != 'function') input = of(input)?

It appears as though a popular approach to 'overloading' is to add an options object whereby a user can specify a type of the input. Another approach I have seen is to just accept an object with optional parameters:

function x(input){
    if (input.observable) input = input.observable;
    if (input.raw) input = input.raw
}

Of course, I could just break it up into a normal function and an async function or require the caller to supply an observable. However, I like my approach - I just want to know if there are any pitfalls I'm not seeing.

TAB
  • 1,944
  • 8
  • 28
  • 45
BobtheMagicMoose
  • 2,246
  • 3
  • 19
  • 27

1 Answers1

0

I decided that this was anti-pattern and that it is better to not take observables as an input unless there's a good reason to do so. Instead, I changed the function call to be input$.pipe(switchmap(input=> x(input))). That way, the async input is left within the calling class.

BobtheMagicMoose
  • 2,246
  • 3
  • 19
  • 27