Questions tagged [observable]

An observable is typically a programming construct that can be "watched" by other parts of the code, called the "observers". Different frameworks and programming languages have different implementations for observables, so this tag should typically be used in conjunction with others.

An observable is typically a programming construct that can be "watched" by other parts of the code, called the "observers". Different frameworks and programming languages have different implementations for observables, so this tag should typically be used in conjunction with others.

An observer may be any object that implements interface Observer. An observable object can have one or more observers. After an observable instance changes, an application calling the Observable's “notification” method causes all of its observers to be notified of the change by a call to their update method.

References

9527 questions
1020
votes
29 answers

Angular/RxJS When should I unsubscribe from `Subscription`

When should I store the Subscription instances and invoke unsubscribe() during the ngOnDestroy life cycle and when can I simply ignore them? Saving all subscriptions introduces a lot of mess into component code. HTTP Client Guide ignore…
282
votes
7 answers

Delegation: EventEmitter or Observable in Angular

I am trying to implement something like a delegation pattern in Angular. When the user clicks on a nav-item, I would like to call a function which then emits an event which should in turn be handled by some other component listening for the…
the_critic
  • 12,720
  • 19
  • 67
  • 115
263
votes
15 answers

Return an empty Observable

The function more() is supposed to return an Observable from a get request export class Collection { public more = (): Observable => { if (this.hasMore()) { return this.fetch(); } else { // return empty observable …
Murhaf Sousli
  • 12,622
  • 20
  • 119
  • 185
215
votes
10 answers

When should we use Observer and Observable?

An interviewer asked me: What is Observer and Observable and when should we use them? I wasn't aware of these terms, so when I got back home and started Googling about Observer and Observable, I found some points from different resources: 1)…
Ravi
  • 30,829
  • 42
  • 119
  • 173
196
votes
7 answers

How can I `await` on an Rx Observable?

I'd like to be able to await on an observable, e.g. const source = Rx.Observable.create(/* ... */) //... await source; A naive attempt results in the await resolving immediately and not blocking execution Edit: The pseudocode for my full intended…
CheapSteaks
  • 4,821
  • 3
  • 31
  • 48
170
votes
3 answers

RxJS: How would I "manually" update an Observable?

I think I must be misunderstanding something fundamental, because in my mind this should be the most basic case for an observable, but for the life of my I can't figure out how to do it from the docs. Basically, I want to be able to do this: //…
LiamD
  • 1,958
  • 2
  • 13
  • 13
169
votes
6 answers

How to create an Observable from static data similar to http one in Angular?

I am having a service that has this method: export class TestModelService { public testModel: TestModel; constructor( @Inject(Http) public http: Http) { } public fetchModel(uuid: string = undefined): Observable { …
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
160
votes
7 answers

Difference between Java 8 streams and RxJava observables

Are Java 8 streams similar to RxJava observables? Java 8 stream definition: Classes in the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements.
rahul.ramanujam
  • 5,608
  • 7
  • 34
  • 56
155
votes
5 answers

How to catch exception correctly from http.request()?

Part of my code: import {Injectable} from 'angular2/core'; import {Http, Headers, Request, Response} from 'angular2/http'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/map'; @Injectable() export class myClass { …
Nick
  • 9,735
  • 7
  • 59
  • 89
152
votes
8 answers

How to check the length of an Observable array

In my Angular 2 component I have an Observable array list$: Observable; In my Template I have
No records found.
151
votes
6 answers

Creating and returning Observable from Angular 2 Service

This is more of a "best practices" question. There are three players: a Component, a Service and a Model. The Component is calling the Service to get data from a database. The Service is using: this.people = http.get('api/people.json').map(res =>…
Joseph Genchik
  • 1,821
  • 3
  • 15
  • 19
140
votes
6 answers

How can I create an observable with a delay

Question For testing purposes, I'm creating Observable objects that replace the observable that would be returned by an actual http call with Http. My observable is created with the following code: fakeObservable = Observable.create(obs => { …
Adrien Brunelat
  • 4,492
  • 4
  • 29
  • 42
139
votes
8 answers

Angular 4+ ngOnDestroy() in service - destroy observable

In an angular application we have ngOnDestroy() lifecycle hook for a component / directive and we use this hook to unsubscribe the observables. I want to clear / destory observable that are created in an @injectable() service. I saw some posts…
mperle
  • 3,342
  • 8
  • 20
  • 34
136
votes
10 answers

How do I return the response from an Observable/http/async call in angular?

I have service which returns an observable which does an http request to my server and gets the data. I want to use this data but I always end up getting undefined. What's the problem? Service: @Injectable() export class EventService { …
eko
  • 39,722
  • 10
  • 72
  • 98
131
votes
11 answers

How to make one Observable sequence wait for another to complete before emitting?

Say I have an Observable, like so: var one = someObservable.take(1); one.subscribe(function(){ /* do something */ }); Then, I have a second Observable: var two = someOtherObservable.take(1); Now, I want to subscribe() to two, but I want to make…
Stephen
  • 18,827
  • 9
  • 60
  • 98
1
2 3
99 100