Observables : Cancel previous http request on new subscription call

I would use a subject to keep everything reactive. In your template html listen to change events and emit a new value to the subject. <searchBar (change)=”search$.next($event.target.value)” /> then in your component: this.subscription = this.search$.pipe( debounceTime(800), distinctUntilChanged(), switchMap(searchText=>http.post(‘api_link’, {searchText}) }).subscribe(response=>{ this.response = response. }); The switchMap will cancel any HTTP request that hasn’t completed if … Read more

Get route query params

update (2.0.0 final) (somepath/:someparam/someotherpath) you can subscribe to them using _router.queryParams.subscribe(…): export class HeroComponent implements OnInit { constructor(private _activatedRoute: ActivatedRoute, private _router:Router) { _activatedRoute.queryParams.subscribe( params => console.log(‘queryParams’, params[‘st’])); original If you want queryParams instead of route params (somepath/:someparam/someotherpath) you can subscribe to them using _router.routerState.queryParams.subscribe(…): export class HeroComponent implements OnInit { constructor(private _activatedRoute: ActivatedRoute, private … Read more

Difference between new Observable(…) and Rx.Observable.create(…)?

There is no difference. Observable.create calls new Observable. As the manual says, Observables are created using Rx.Observable.create or a creation operator <…> Rx.Observable.create is an alias for the Observable constructor Observable.create is conventionally used, probably because it reads better in chains and conforms with other Observable static methods that create observables, too. The difference may … Read more

Angular2 Observable – Await multiple function calls before proceeding

I have been doing this with forkJoin import {Observable} from ‘rxjs/Observable’; import ‘rxjs/add/observable/forkJoin’; Observable.forkJoin( this.http.get(‘./friends.json’).map((res: Response) => res.json()), this.http.get(‘./customer.json’).map((res: Response) => res.json()) ) .subscribe(res => this.combined = {friends: res[0].friends, customer: res[1]}); Some more info here: http://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http

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

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

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

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