0

If i check RxJS subscribe method I can see that:

    subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;

so I write example init function like this:

  private init(): void{
this.dataBaseService.fetchPersons().subscribe(
   (persons: Person[]) => {
    this.behaviorSubject.next(persons);
    this.subject.next(persons);
  },
  error => console.error(error),
  () => console.log('Complete!')
);

}

is it required in Typescript to provide lambda function to argument? Can I create function somewhere else and provide it as argument?

for example this function:

       (persons: Person[]) => {
    this.behaviorSubject.next(persons);
    this.subject.next(persons);
  }

create in upper class and then provide it as argument.

Ok so I tried to create a method inside a class:

  someFunction( persons: Person[] ){
this.behaviorSubject.next(persons);
this.subject.next(persons);

}

and tried to pass it to init function

    private init2(): void {
  this.dataBaseService.fetchPersons().subscribe(
    this.someFunction(),
    error => void,
    () => console.log('Complete!');
  )
}

And I receive error:

An argument for 'persons' was not provided.

What kind of argument I have to provide if its initialisation of this upper method?

Dark Furby
  • 97
  • 1
  • 8

1 Answers1

2

You need to pass your function without the () or it instantly gets called:

someFunction( persons: Person[]) {
    this.behaviorSubject.next(persons);
    this.subject.next(persons);
}
    
private init2(): void {
    this.dataBaseService.fetchPersons().subscribe(
        this.someFunction, // <- pass it without ()
        error => void,
        () => console.log('Complete!')
    )
}
    
MauriceNino
  • 6,214
  • 1
  • 23
  • 60
  • Its working thank you! But i dont know how its working, will behaviorSubject and subject receive persons argument? – Dark Furby Aug 06 '20 at 14:01
  • Yes it will. If you are unsure, or just want to write clearer code, you could also pass `person => this.someFunction(person)` instead of `this.someFunction` @DarkFurby – MauriceNino Aug 06 '20 at 14:02
  • I'd be careful about that because the use of ```this``` inside the function might break in certain circumstances. I found that it's always better to declare your function as a field like ```someFunction = (...) => {...}``` when planning on using it as a variable/parameter – h0ss Aug 06 '20 at 14:03
  • I have used both countless times and have never seen it break somehow, but I would be more than open to get shown otherwise- Do you have an example? @h0ss – MauriceNino Aug 06 '20 at 14:05
  • Here is an example of this scoping gone bad, I guess in OPs example it will be fine but it is always useful to know the limitations of certain approaches https://stackoverflow.com/questions/20627138/typescript-this-scoping-issue-when-called-in-jquery-callback – h0ss Aug 06 '20 at 14:12
  • Thanks for the link! That's an interesting problem, but as you said yourself - probably not much applicable to OPs question. Thanks though @h0ss – MauriceNino Aug 06 '20 at 14:16