Upon examining the RxJS subscribe method, I noticed that:
subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;
So, I decided to create an example initialization function like this:
private initialize(): void{
this.dataBaseService.fetchPersons().subscribe(
(persons: Person[]) => {
this.behaviorSubject.next(persons);
this.subject.next(persons);
},
error => console.error(error),
() => console.log('Complete!')
);
}
In Typescript, is it necessary to provide a lambda function as an argument? Can I create a function elsewhere and then use it as an argument?
For instance, consider this function:
(persons: Person[]) => {
this.behaviorSubject.next(persons);
this.subject.next(persons);
}
Create it in a higher class and subsequently pass it as an argument.
I attempted to define a method within a class:
someFunction( persons: Person[] ){
this.behaviorSubject.next(persons);
this.subject.next(persons);
}
and tried to pass it to the init function
private initialize2(): void {
this.dataBaseService.fetchPersons().subscribe(
this.someFunction(),
error => void,
() => console.log('Complete!');
)
}
However, I encountered an error:
An argument for 'persons' was not provided.
What type of argument do I need to provide if it's the initialization of this parent method?