Answer the question
In order to leave comments, you need to log in
Rxjs - how to double subscribe?
Hello.
Please tell me how to implement this:
let get = () => {
return this.http.get(url)
.subscribe((responce: ResponceObject) => {...});
}
get()
.subscribe((responce: ResponceObject) => {...});
Answer the question
In order to leave comments, you need to log in
The observer doesn't do any work until the subscribe function has been called. The process of calling subscribe is called "subscribing". As soon as the subscribe function was called (i.e., the observer was subscribed), the matter went and the observer began its work. Until subscribe is called, the observer is just a recipe for what to do when data arrives.
The subscribe function itself returns a destructor. The destructor is something that needs to be executed if suddenly it was necessary to stop the observer (for example, to interrupt the HttpConnection).
The code below returns a destructor function
let get = () => {
return this.http.get(url)
.subscribe((response: ResponseObject) => {...});
}
let get = () => {
return this.http.get(url)
.pipe(
tap((response: ResponseObject) => {...})
);
}
// а дальше где-то в коде сделать
service.get().subscribe((response: ResponseObject) => {...})
service.get()
.pipe(
tap(...),
map(...),
whatever(...),
).subscribe((response: ResponseObject) => {...})
const observable = service.get();
observable.subscribe((result) => {...}); //выполнит запрос к серверу
observable.subscribe((result) => {...}); //выполнит запрос к серверу еще раз
// запрос к серверу будет инициироваться каждый раз когда будет выполняться функция subscribe
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question