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
121
votes
8 answers

flatMap, mergeMap, switchMap and concatMap in rxjs?

Someone, please explain the difference between SwitchMap and FlatMap in terms of Javascript ( in angular perspective, rxjs 5) In my understanding. SwitchMap only emits the latest observable value and cancels the previous observable. flatMap collects…
xkeshav
  • 53,360
  • 44
  • 177
  • 245
121
votes
4 answers

Using an array from Observable Object with ngFor and Async Pipe Angular 2

I am trying to understand how to use Observables in Angular 2. I have this service: import {Injectable, EventEmitter, ViewChild} from '@angular/core'; import {Observable} from "rxjs/Observable"; import {Subject} from "rxjs/Subject"; import…
C. Kearns
  • 1,531
  • 3
  • 13
  • 18
117
votes
2 answers

Difference between .unsubscribe to .take(1)

I wonder, if there is any difference in performance between using .take(1) and .unsubscribe when unsubscribe is used right after the subscription: var observable = Rx.Observable.interval(100); First: var subscription =…
TheUnreal
  • 23,434
  • 46
  • 157
  • 277
117
votes
4 answers

Promise.all behavior with RxJS Observables?

In Angular 1.x I would sometimes need to make multiple http requests and do something with all the responses. I would throw all the promises in an array and call Promise.all(promises).then(function (results) {...}). Angular 2 best practices seem to…
Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
109
votes
8 answers

async/await in Angular `ngOnInit`

I’m currently evaluating the pros ‘n’ cons of replacing Angular’s resp. RxJS’ Observable with plain Promise so that I can use async and await and get a more intuitive code style. One of our typical scenarios: Load some data within ngOnInit. Using…
qqilihq
  • 10,794
  • 7
  • 48
  • 89
104
votes
22 answers

TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

I am trying to map from a service call but getting an error. Looked at subscribe is not defined in angular 2? and it said that in order to subscribe we need to return from inside the operators. I have return statements as well. Here's my…
Aakash Thakur
  • 3,837
  • 10
  • 33
  • 64
103
votes
2 answers

Chaining RxJS Observables from http data in Angular2 with TypeScript

I'm currently trying to teach myself Angular2 and TypeScript after happily working with AngularJS 1.* for the last 4 years! I have to admit I am hating it but I am sure my eureka moment is just around the corner... anyway, I have written a service…
Mike Sav
  • 14,805
  • 31
  • 98
  • 143
99
votes
2 answers

fromPromise does not exist on type Observable

In Angular 2 using rxjs I was trying to convert a Promise to Observable. As many of online guides showed I used fromPromise on Observable. Which throws error: Property 'fromPromise' does not exist on type 'typeof Observable'. Observable was…
Ahmad
  • 2,629
  • 3
  • 22
  • 27
96
votes
6 answers

Get current value from Observable without subscribing (just want value one time)

How can I get the current value from an Observable without subscribing to it? I just want the current value one time and not new values as they are coming in.
EricC
  • 5,720
  • 13
  • 52
  • 71
94
votes
8 answers

Angular 2: Convert Observable to Promise

Q) How do I convert the following observable to a promise so I can call it with .then(...)? My method I want to convert to a promise: this._APIService.getAssetTypes().subscribe( assettypes => { …
Dave
  • 5,283
  • 7
  • 44
  • 66
93
votes
6 answers

How to return observable from subscribe

I'm trying to return an observable when I get a certain value in a subscriber, but I fail miserably. This is the code: canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable { // get route to be activated …
Dragos Ionescu
  • 1,163
  • 1
  • 9
  • 12
89
votes
7 answers

Angular 6 View is not updated after changing a variable within subscribe

Why is the view not being updated when a variable changes within a subscribe? I have this code: example.component.ts testVariable: string; ngOnInit() { this.testVariable = 'foo'; this.someService.someObservable.subscribe( () =>…
89
votes
3 answers

How to get data from observable in angular2

I am trying to print the result of http call in Angular using rxjs Consider the following code import { Component, Injectable, OnInit } from '@angular/core'; import { Http, HTTP_PROVIDERS } from '@angular/http'; import…
testing
  • 2,303
  • 4
  • 20
  • 32
87
votes
4 answers

Is it a good practice using Observable with async/await?

I am using angular 2 common http that return an Observable, but I face with a problem that my code likes a mesh when I use nested Observable call: this.serviceA.get().subscribe((res1: any) => { this.serviceB.get(res1).subscribe((res2: any) => { …
Thach Huynh
  • 1,173
  • 2
  • 12
  • 24
87
votes
5 answers

Get previous value of an observable in subscribe of same observable

Is it possible in knockout to get the current value of an observable within a subscription to that observable, before it receives the new value? Example: this.myObservable = ko.observable(); this.myObservable.subscribe(function(newValue){ //I'd…
KodeKreachor
  • 8,852
  • 10
  • 47
  • 64