rxjs6

Sequential cascade Observables according result from previous response and remap to new data object

六月ゝ 毕业季﹏ 提交于 2020-05-15 05:09:50
问题 I have difficulties using the power of Observables i.e. RxJs 6 to correctly pipe, tap, map, mergemap or whatever my HttpClient requests. The version hell with all the different functions makes it not very easy... So what I need is to first make a REST call, then depending on the result probably do a second REST call and map the potentially two received data objects in one new data object. The function should return an Observable . So currently, I solved it with a subject that I am be able to

Sequential cascade Observables according result from previous response and remap to new data object

纵然是瞬间 提交于 2020-05-15 05:07:27
问题 I have difficulties using the power of Observables i.e. RxJs 6 to correctly pipe, tap, map, mergemap or whatever my HttpClient requests. The version hell with all the different functions makes it not very easy... So what I need is to first make a REST call, then depending on the result probably do a second REST call and map the potentially two received data objects in one new data object. The function should return an Observable . So currently, I solved it with a subject that I am be able to

How to propagate errors through catchError() properly?

柔情痞子 提交于 2020-05-10 03:58:50
问题 I wrote a function that is pipe -able: HandleHttpBasicError<T>() { return ((source:Observable<T>) => { return source.pipe( catchError((err:any) => { let msg = ''; if(err && err instanceof HttpErrorResponse) { if(err.status == 0) msg += "The server didn't respond"; } throw { err, msg } as CustomError }) ) }) } I can use this function this way in my HttpService : checkExist(id:string) { return this.http.head<void>(environment.apiUrl + 'some_url/' + id) .pipe( HandleHttpBasicError(), catchError(

How to propagate errors through catchError() properly?

送分小仙女□ 提交于 2020-05-10 03:58:05
问题 I wrote a function that is pipe -able: HandleHttpBasicError<T>() { return ((source:Observable<T>) => { return source.pipe( catchError((err:any) => { let msg = ''; if(err && err instanceof HttpErrorResponse) { if(err.status == 0) msg += "The server didn't respond"; } throw { err, msg } as CustomError }) ) }) } I can use this function this way in my HttpService : checkExist(id:string) { return this.http.head<void>(environment.apiUrl + 'some_url/' + id) .pipe( HandleHttpBasicError(), catchError(

How do I merge an array of Observables with RxJS 6.x and Node.js?

老子叫甜甜 提交于 2020-04-16 03:56:06
问题 For learning purposes, I'm creating a Node app that will need to take x RxJS observables from an array and combine into a single stream of events. I want to know when events occur in any observable, in any order (not in any sequence or full completion). I feel it should be in a single merged stream of events. Basically, the first event that comes through from any observable will complete. For this, I felt merge() will do the trick. As merge doesn't take arrays directly as a parameter, I'm

When should we use the RxJS tap operator?

馋奶兔 提交于 2020-03-01 03:38:29
问题 I do not understand from the docs. Could anyone explain it to me? 回答1: Most of the operators are working in streamed sequence, for example: source$.pipe( map((a: string) => changeAndReturnArray(a)), filter((b: string[]) => giveMeOnlySymbolsThatAreAfterNInAlphabet(b)), switchMap((c: string[]) => putToSomeObservable(c)) .... ); In that example you are not 'breaking' the stream, or jumping outside of it to do some external action. Jumping outside of stream is possible with 'tap' operator, where

Failing expect() inside subscribe() does not mark test as invalid

扶醉桌前 提交于 2020-02-24 10:18:49
问题 We recently upgraded to Angular 6.0.3, RxJs 6.2.0 and jest 23.1.0 (upgrading from RxJS 5 & Angular 4). There seems to be a problem with Jest & RxJs as failing expect-statements inside a subscribe-Block do not mark the test as failed. Here's a minimal example: it("should fail", () => { const obs = Observable.create((observer) => { observer.next(false); }); obs.subscribe((value) => { console.log(value); // => false expect(value).toBeTruthy(); }); }); The expect-Statement gets executed, but the

RxJS dynamic source of observables

前提是你 提交于 2020-02-04 05:30:48
问题 I want to create an Observable of many observables (merge them). This could be achieved with merge(...arrayOfObservables) . The problem is that some time this array will be changed and the observable should subscribe to the new observables, too. 回答1: You can push new Observables to an array and then emit the array and subscribe to them with switchMap . import { of, merge, BehaviorSubject } from 'rxjs'; import { switchMap } from 'rxjs/operators'; const s = new BehaviorSubject([of(1), of(2), of

Polling server after each server response + delay

巧了我就是萌 提交于 2020-01-15 07:45:11
问题 I am working on an effect, that will be polling server. What I want to achieve is as follows: 1) Send GET request to server 2) After response is received, wait 3 seconds 3) Send same GET request 4) After response is received, wait 3 seconds 5) Send same GET request ... and so on. The code that I have now, doesn't quite work, as it polls server every 3 seconds, no matter if response was received or not: @Effect() pollEntries$ = this.actions$.pipe( ofType(SubnetBrowserPageActions

RXJS - multiple consecutive http requests

限于喜欢 提交于 2020-01-15 04:40:09
问题 source<http> .pipe( switchMap(d => this.http.get(d)) .pipe( switchMap(j => this.http.get(j)) ) ) .subscribe() Hello, I need to make 3 http requests consecutively where each call contains data for the next call.. are nested switch maps the best practice for this case? 回答1: You don't need to nest them. You can simply chain them: source<http> .pipe( switchMap(d => this.http.get(d)), switchMap(j => this.http.get(j)) ) .subscribe() Apart from that, using multiple switchMap is the way to go. 回答2: