Observables: Complete vs finally vs done

Finally always happens whenever an observable sequence terminates (including errors); completed only happens when it terminates without errors. Finally: Invokes a specified action after the source observable sequence terminates gracefully or exceptionally. https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/finally.md OnCompleted: An Observable calls this method after it has called onNext for the final time, if it has not encountered any errors. … Read more

RxJS emit array items over time?

Three ways to do it, with RxJS version 6 : 1. Using concatMap import { from, of, pipe } from ‘rxjs’; import { concatMap, delay } from ‘rxjs/operators’; const array = [1, 2, 3, 4, 5]; from(array) .pipe( concatMap(val => of(val).pipe(delay(1000))), ) .subscribe(console.log); 2. Using zip and interval import { from, pipe, interval } from … Read more

Testing Observables with jest

There are some good examples in the Jest documentation about passing in an argument for the test. This argument can be called to signal a passing test or you can call fail on it to fail the test, or it can timeout and fail. https://jestjs.io/docs/en/asynchronous.html https://alligator.io/testing/asynchronous-testing-jest/ Examples Notice I set the timeout to 1500ms const … Read more

Filter undefined from RxJS Observable

Strictly speaking, using filter alone to remove undefined values doesn’t do everything that you need. It filters out the undefined values, but does not change the type to indicate that undefined is no longer a possible value. In the example: const obs = new Subject<string | undefined>; const stringObs = obs.pipe(filter(x => x !== undefined)) … Read more

Observable.forkJoin() doesn’t execute

forkJoin() requires all source Observables to emit at least once and to complete. This following demo completes as expected: const source = forkJoin( from([1,2,3]), from([9,8,7,6]) ).subscribe( x => console.log(‘GOT:’, x), err => console.log(‘Error:’, err), () => console.log(‘Completed’) ); Live demo: https://stackblitz.com/edit/rxjs-urhkni GOT: 3,6 Completed Jan 2019: Updated for RxJS 6

Why would I use RxJS interval() or timer() polling instead of window.setInterval()?

Advantage of RxJS: Laziness You can create your Observables and until you call subscribe nothing is happening. Observable = pure function. This gives you more control, easier reasoning and allows for next point… Composability You can combine interval/timer with other operators creating custom logic very easily in unified way – for example you can map, … Read more

How to Stop observable.timer in Angular2

There’re are basically two ways: call unsubscribe() on the Subscription object returned from the subscribe() call . use an operator To just unsubscribe you could do it like this. ngOnInit() { this.subscription = timer(100, 100).subscribe(t => { this.setFormData(); }); } private setFormData() { … this.subscription.unsubscribe(); } Or you can use Subject to complete the Observable … Read more

How use async service into angular httpClient interceptor

If you need to invoke an async function within interceptor then the following approach can be followed using the rxjs from operator. import { MyAuth} from ‘./myauth’ import { from, lastValueFrom } from “rxjs”; @Injectable() export class AuthInterceptor implements HttpInterceptor { constructor(private auth: MyAuth) {} intercept(req: HttpRequest<any>, next: HttpHandler) { // convert promise to observable … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)