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

In my case the error occurred only during e2e tests. It was caused by throwError in my AuthenticationInterceptor. I imported it from a wrong source because I used WebStorm’s import feature. I am using RxJS 6.2. Wrong: import { throwError } from ‘rxjs/internal/observable/throwError’; Correct: import { throwError } from ‘rxjs’; Here the full code of … Read more

How to parse xml in Angular 2

Based on the library http://goessner.net/download/prj/jsonxml/, I implemented a sample to receive XML data and parse them into an Angular2 application: var headers = new Headers(); (…) headers.append(‘Accept’, ‘application/xml’); return this.http.get(‘https://angular2.apispark.net/v1/companies/’, { headers: headers }).map(res => JSON.parse(xml2json(res.text(),’ ‘))); To be able to use the library, you need to parse first the XML string: var parseXml; if … Read more

How to create an RXjs RetryWhen with delay and limit on tries

You need to apply the limit to the retry signal, for instance if you only wanted 10 retries: loadSomething(): Observable<SomeInterface> { return this.http.get(this.someEndpoint, commonHttpHeaders()) .retryWhen(errors => // Time shift the retry errors.delay(500) // Only take 10 items .take(10) // Throw an exception to signal that the error needs to be propagated .concat(Rx.Observable.throw(new Error(‘Retry limit exceeded!’)) … Read more

Angular 2 cache observable http result data [duplicate]

If you lean into observables as a means of sharing data, you can adopt the following approach: import { Injectable } from ‘@angular/core’; import { HttpClient } from ‘@angular/common/http’; import { Observable, ReplaySubject } from ‘rxjs’; import { SomeModel } from ‘path/to/it’; @Injectable({ providedIn: ‘root’ }) export class CachedService { private dataSubject = new ReplaySubject<SomeModel>(1); … Read more

RxJS Observables nested subscriptions?

As mentioned in comments, you are looking for the flatMap operator. You can find more details in previous answers : How to do the chain sequence in rxjs Why do we need to use flatMap? Your example would read as : this.returnsObservable1(…) .flatMap(success => this.returnsObservable2(…)) .flatMap(success => this.returnsObservable3(…)) .subscribe(success => {(…)});

Difference between .unsubscribe to .take(1)

Each serves a different purpose so it’s hard to compare them. In general if you take this source: const source = range(1,3); … and consume it with subscribe() followed immediately by unsubscribe(): source.subscribe( console.log, undefined, () => console.log(‘complete’) ).unsubscribe(); … then all values from source are going to be emitted even though we called unsubscribe() … Read more

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

Chaining Observables in sequence, as you want to do in your code Concerning your code example, if you want to chain Observables (trigger another after the previous emits), use flatMap (or switchMap) for this purpose : this.serviceA.get() .flatMap((res1: any) => this.serviceB.get()) .flatMap((res2: any) => this.serviceC.get()) .subscribe( (res3: any) => { …. }); This one is … Read more

tech