How to ‘wait’ for two observables in RxJS

Last Update: Mar, 2022. RxJS v7: combineLatestWith From reactiveX documentation: Whenever any input Observable emits a value, it computes a formula using the latest values from all the inputs, then emits the output of that formula. // Observables to combine const name$ = this._personService.getName(id); const document$ = this._documentService.getDocument(); name$.pipe( combineLatestWith($document) ) .subscribe(([name, document]) => { … Read more

Using an array from Observable Object with ngFor and Async Pipe Angular 2

Here’s an example // in the service getVehicles(){ return Observable.interval(2200).map(i=> [{name: ‘car 1’},{name: ‘car 2’}]) } // in the controller vehicles: Observable<Array<any>> ngOnInit() { this.vehicles = this._vehicleService.getVehicles(); } // in template <div *ngFor=”let vehicle of vehicles | async”> {{vehicle.name}} </div>

How to make one Observable sequence wait for another to complete before emitting?

A couple ways I can think of import {take, publish} from ‘rxjs/operators’ import {concat} from ‘rxjs’ //Method one var one = someObservable.pipe(take(1)); var two = someOtherObservable.pipe(take(1)); concat(one, two).subscribe(function() {/*do something */}); //Method two, if they need to be separate for some reason var one = someObservable.pipe(take(1)); var two = someOtherObservable.pipe(take(1), publish()); two.subscribe(function(){/*do something */}); one.subscribe(function(){/*do … Read more

How to throw error from RxJS map operator (angular)

Just throw the error inside the map() operator. All callbacks in RxJS are wrapped with try-catch blocks so it’ll be caught and then sent as an error notification. This means you don’t return anything and just throw the error: map(res => { if (res.bearerToken) { return this.saveJwt(res.bearerToken); } else { throw new Error(‘Valid token not … Read more

When to use asObservable() in rxjs?

When to use Subject.prototype.asObservable() The purpose of this is to prevent leaking the “observer side” of the Subject out of an API. Basically to prevent a leaky abstraction when you don’t want people to be able to “next” into the resulting observable. Example (NOTE: This really isn’t how you should make a data source like … Read more

What is “callback hell” and how and why does RX solve it?

1) What is a “callback hell” for someone who does not know javascript and node.js ? This other question has some examples of Javascript callback hell: How to avoid long nesting of asynchronous functions in Node.js The problem in Javascript is that the only way to “freeze” a computation and have the “rest of it” … Read more

Angular 4+ ngOnDestroy() in service – destroy observable

OnDestroy lifecycle hook is available in providers. According to the docs: Lifecycle hook that is called when a directive, pipe or service is destroyed. Here’s an example: @Injectable() class Service implements OnDestroy { ngOnDestroy() { console.log(‘Service destroy’) } } @Component({ selector: ‘foo’, template: `foo`, providers: [Service] }) export class Foo implements OnDestroy { constructor(service: Service) … Read more

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