Search code examples
rxjs6debouncing

Not able to understand debounce parameters in rxjs


I wanted to understand the intellisense displayed in the visual code which got me more confused

enter image description here

debounce(
durationSelector: (value: {}) => SubscribableOrPromise<any>
): MonoTypeOperatorFunction<{}>

from the above example, I think I understood the following:

  1. debounce is the name of the function
  2. durationSelector is a function which that a single parameter:

    a. value which takes Object as parameter

    b. SubscribableOrPromise<any> is the type that the function is going to return

  3. MonoTypeOperatorFunction<{}>, not able to understand how a function can return MonoTypeOperatorFunction<{}> when it already returns SubscribableOrPromise<any>, this is where I am confused and not able to understand

Solution

  • not able to understand how a function can return MonoTypeOperatorFunction<{}> when it already returns SubscribableOrPromise<any>

    That's not what is being described here, you don't have one function that returns MonoTypeOperatorFunction and also SubscribableOrPromise. You have two functions:

    1. debounce; and
    2. the durationSelector argument to debounce (which may be anonymous in your code).

    debounce returns MonoTypeOperatorFunction<{}>, so the result can be passed to pipe (which takes OperatorFunctions, of which that's a subtype).

    debounce requires the function as you're passing as an argument to it to return SubscribableOrPromise<any>, the line:

    (value: {}) => SubscribableOrPromise<any>
    

    is the declaration of the argument, durationSelector; it's a function type.

    I'd recommend reading up on "higher-order functions", as that's a really important concept to understand if you're going to use RxJS.