Get current value from Observable without subscribing (just want value one time)

Quick answer: …I just want the current value one time and not new values as they are coming in… You will still use subscribe, but with pipe(take(1)) so it gives you one single value. eg. myObs$.pipe(take(1)).subscribe(value => alert(value)); Also see: Comparison between first(), take(1) or single() Longer answer: The general rule is you should only … Read more

fromPromise does not exist on type Observable

UPDATE: As of rxjs 6.0.0-beta.3, operators and observable creators should be imported from rxjs. Furthermore, fromPromise is not part of the public API anymore and its wrapped in the from method. TL;DR; UPDATE For rxjs 6.0.0 use: import { from } from ‘rxjs’; var observableFromPromise = from(promiseSrc); UPDATE: After the release of the pipeable operators … Read more

TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

In my case the error occurred only during e2e tests. It was caused by throwError in my AuthenticationInterceptor. I imported it from a wrong source because I used WebStorm’s import feature. I am using RxJS 6.2. Wrong: import { throwError } from ‘rxjs/internal/observable/throwError’; Correct: import { throwError } from ‘rxjs’; Here the full code of … Read more

Chaining RxJS Observables from http data in Angular2 with TypeScript

For your use case, I think that the flatMap operator is what you need: this.userData.getUserPhotos(‘123456’).flatMap(data => { this.userPhotos = data; return this.userData.getUserInfo(); }).subscribe(data => { this.userInfo = data; }); This way, you will execute the second request once the first one is received. The flatMap operator is particularly useful when you want to use the … Read more

Difference between the methods .pipe() and .subscribe() on a RXJS observable

The pipe method is for chaining observable operators, and the subscribe is for activating the observable and listening for emitted values. The pipe method was added to allow webpack to drop unused operators from the final JavaScript bundle. It makes it easier to build smaller files. For example if I have this function, let’s call … Read more

Why do we need to use flatMap?

[‘a’,’b’,’c’].flatMap(function(e) { return [e, e+ ‘x’, e+ ‘y’, e+ ‘z’ ]; }); //[‘a’, ‘ax’, ‘ay’, ‘az’, ‘b’, ‘bx’, ‘by’, ‘bz’, ‘c’, ‘cx’, ‘cy’, ‘cz’] [‘a’,’b’,’c’].map(function(e) { return [e, e+ ‘x’, e+ ‘y’, e+ ‘z’ ]; }); //[Array[4], Array[4], Array[4]] You use flatMap when you have an Observable whose results are more Observables. If you have … Read more

Difference between .unsubscribe to .take(1)

Each serves a different purpose so it’s hard to compare them. In general if you take this source: const source = range(1,3); … and consume it with subscribe() followed immediately by unsubscribe(): source.subscribe( console.log, undefined, () => console.log(‘complete’) ).unsubscribe(); … then all values from source are going to be emitted even though we called unsubscribe() … Read more

Promise.all behavior with RxJS Observables?

The more straightforward alternative for emulating Promise.all is to use the forkJoin operator (it starts all observables in parallel and join their last elements): documentation related link: Cf. RxJS: concat three promises, distinguish results A bit out of scope, but in case it helps, on the subject of chaining promises, you can use a simple … Read more

Angular 4 Interceptor retry requests after token refresh

My final solution. Works with parallel requests. UPDATE: The code updated with Angular 9 / RxJS 6, error handling and fix looping when refreshToken fails import { HttpRequest, HttpHandler, HttpInterceptor, HTTP_INTERCEPTORS } from “@angular/common/http”; import { Injector } from “@angular/core”; import { Router } from “@angular/router”; import { Subject, Observable, throwError } from “rxjs”; import … 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

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