1

I found this code online and trying to understand it.

public searchFunctionFactory($index: any): (text: Observable<string>) => Observable<any[]> {


    //Create a function that considers the specified $index parameter value
    let getCities = (text$: Observable<string>) => 
        text$
            .debounceTime(300)
            .distinctUntilChanged()
            .switchMap( query => {

                //some logic involving $index here
                //...

                //query.length < 2 ? [] : this.apiService.getCities(query).catch(() => {
                //return Observable.of([]);
            });

    //Return that "custom" function, which will in turn be called by the ngbTypescript component
    return getCities;
}

My question revolves around the method signature.

public searchFunctionFactory($index: any): (text: Observable<string>) => Observable<any[]>

I understand

$index: any is an input parameter of type Any.

The function returns Observable an observable of any.

But what is : (text: Observable<string>)

Is it a second parameter?

If its a second parameter shouldn't it be

public searchFunctionFactory($index: any ,text: Observable<string>) => Observable<any[]> 
1
  • That is actually a function that returns a function, and it is TypeScript code, not JavaScript. Commented Apr 24, 2019 at 3:17

1 Answer 1

1

That indicates that the return value of the searchFunctionFactory function is a function which takes a parameter of text, whose type is Observable<string>, and calling that function returns an Observable<any[]>. It's a higher-order function.

public searchFunctionFactory($index: any): (text: Observable<string>) => Observable<any[]> {
                        /*   ^^^^^^^^^^^   searchFunctionFactory parameter */
public searchFunctionFactory($index: any): (text: Observable<string>) => Observable<any[]> {
  /* searchFunctionFactory return value     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */

As comment notes, this is not Javascript syntax, but Typescript syntax.

So, if you call

const result = searchFunctionFactory(someVar);

result will be of the type

(text: Observable<string>) => Observable<any[]>

and you could then call it with

const resultTwo = result(someOtherVar)

to get a value whose type is Observable<any[]>.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for the explanation .can i call it like this const result = searchFunctionFactory(someVar)(someOtherVar)()
Don't think so. You could do searchFunctionFactory(someVar)(someOtherVar), thereby calling the returned function immediately, but that returned function does not return a function as well (it returns an Observable), so you couldn't call it too. const observableOfAny = searchFunctionFactory(someVar)(someOtherVar)
Hi Thanks again so it would be on the lines of const observableOfAny = searchFunctionFactory(someVar) and then the usual routine observableOfAny.subscribe(() => do something) ?
What searchFunctionFactory returns is a function, not a observerable - you'd have to call the returned function to get the observable of any. searchFunctionFactory(someVar)(someOtherVar) and then you can call .subscribe or whatever you want on that observable

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.