How can I create an observable with a delay

Using the following imports: import {Observable} from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; import ‘rxjs/add/operator/delay’; Try this: let fakeResponse = [1,2,3]; let delayedObservable = Observable.of(fakeResponse).delay(5000); delayedObservable.subscribe(data => console.log(data)); UPDATE: RXJS 6 The above solution doesn’t really work anymore in newer versions of RXJS (and of angular for example). So the scenario is that I have an array of … Read more

Paginate Observable results without recursion – RxJava

JohnWowUs’ answer is great and helped me understand how to avoid the recursion effectively, but there were some points I was still confused about, so I’m posting my tweaked version. Summary: The individual pages are returned as a Single. Use a Flowable to stream each of the items contained in the pages. This means callers … Read more

flatMap, mergeMap, switchMap and concatMap in rxjs?

Taking this from a previous answer: flatMap/mergeMap – creates an Observable immediately for any source item, all previous Observables are kept alive. Note flatMap is an alias for mergeMap and flatMap will be removed in RxJS 8. concatMap – waits for the previous Observable to complete before creating the next one switchMap – for any … 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

Angular – Make multiple HTTP calls sequentially

This can be achieved using the switchMap operator. This example uses RxJS 5.5+ pipeable operators. import { switchMap } from ‘rxjs/operators’; registerUser(user: User) { return this.utility.getIpAddress().pipe( switchMap(data => { this.ipAddress = data.ip; const body = { UserName: user.UserName, Email: user.Email, UserIP: this.ipAddress, }; return this.http.post(this.registerAPI, body); }) ) } RxJS < 5.5: import { switchMap … Read more

Rxjs One Observable Feeding into Another

For transforming items emitted by an Observable into another Observable, you probably want to use the mergeMap operator. It creates an inner Observable and flattens its result to the outer stream. const source = this.myService .getFoo() .pipe( mergeMap(result => this.myService.getMoo(result)) ) .subscribe(result2 => { // do some stuff }); Here are some flat operators you … Read more

tech