observable

Observable: Why result not in subscribe() success function if preceded by map()?

梦想与她 提交于 2020-01-03 16:50:59
问题 I refer to this post which was really helpful to understand Observables, but there is still something I don't get. In the plunker it refers to, I changed the "src/myfreinds.ts" to see a bit what happen and added some console log: First I left it as the same: export class FriendsList{ result:Array<Object>; constructor(http: Http) { console.log("Friends are being called"); http.get('friends.json') .map(response => { console.log("map"); console.info(response); response.json(); //MISTAKE WAS HERE

Observable errors with Angular2 beta.12 and RxJs 5 beta.3

北慕城南 提交于 2020-01-03 08:53:12
问题 Hello, I am using Angular2 beta 12 running in VS2015. When I update to rxjs from 5.0.0-beta.2 to beta.3 I encounter a range of exceptions generally relating to my promises. E.g. Property map does not exist on type Observable<Response> Property share does not exist in type Observable<Response> Ambient modules declaration cannot specify relative module name Ambient modules cannot be nested in other modules or namespaces. Package.json { "name": "ASP.NET", "version": "0.0.0", "scripts": { "tsc":

How to find an element of an array by id with Observable in Angular2

早过忘川 提交于 2020-01-03 06:58:13
问题 I decided to use Observable instead of Http promises. That is how my Promise service looked: export class MovieService { movies: Movie[] movie: Movie; constructor(private http:Http) { } getMovies(): Promise<Movie[]>{ return this.http.get('http://api.request.com') .toPromise() .then((res:Response) => res.json()['results']) } getMovie(id: number): Promise<Movie> { return this.getMovies() .then(movies => movies.find(movie => movie.id == id)); } } First I fetch an array of movies, and than I find

Async function not working as intended when called at onInit lifecycle hook

谁说胖子不能爱 提交于 2020-01-03 02:00:30
问题 In my project I have a service that should load local database when the app is started. For that the function GetData() is used. I try to use it by calling at the ngOnInit() lifecycle hook. It logs the result in the console, but the property datum appears to be unchanged. However, if I add GetData() method to a button, the property changes and data is displayed in the console as intended. All sources that I looked through suggest that the right way to load the DB if I need it right away is to

Memoize for Observables in Typescript

烈酒焚心 提交于 2020-01-02 12:09:34
问题 I am looking for the best way of implementing an optimization for very expensive method that takes multiple parameters and returns an Observable. Is there an elegant way of doing it? What I am looking for is prettier version of this: class Example { constructor( private databaseService: DatabaseService, private someService: SomeService) expensive(param1: string, param2: string) : Observable<string> { if (isMemoraized(param1,param2) { return Observable.create(observer=> observer.next

How to pass a value inside an observable to (click) handler in Angular2

我怕爱的太早我们不能终老 提交于 2020-01-02 07:06:01
问题 I try to learn observable programming with Angular 2 BUT I don't get one important point - how to pass in the value that is in an observable in the (click) event in the template level (without subscribing the observable in the TS file and turn the observable value into member variable). As lots of Firebase experts suggest this "Wherever possible, try to use | async in angular2 and prevent manual subscriptions!" - reason for not wanting to manually subscribe to task$ and turn it into an array.

Observable.of turn async

风格不统一 提交于 2020-01-02 05:40:09
问题 I'm about to mock a http call wrapped into observable. My initial idea was to simply use Observable.of similar to Promise.resolve , but it does not seem to work as I expected: Rx.Observable.of('of1').subscribe(e => console.log(e)); console.log('of2'); Rx.Observable.from(Promise.resolve('from1')).subscribe(e => console.log(e)); console.log('from2'); <script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.6/dist/global/Rx.umd.js"></script> It seems that Observable.of runs synchronously while

Observable retry / retryWhen with flatmap

送分小仙女□ 提交于 2020-01-01 11:14:10
问题 I have the following code to get id and then data related to the id. process(): Observable<any> { let id: number; return this.getId().flatMap( (response: number) => { id = response; return this.getData(id) } ).flatMap( (data: any) => { return Observable.of(data); } ); } getData(id: number): Observable<any> { // retry if response.status is not complete return this.service.getData(id).map(response => return response.data); } I would like to add retry logic on getData method until response

Access authenticated data with AngularFire2

老子叫甜甜 提交于 2020-01-01 07:28:06
问题 I am using AngularFire2 in an Angular2 project to do a very basic task, login and then show the logged in users information on the page. The seemingly simple snag I've hit is that I need to know the users UID to request that data from Firebase (my permissions state can only access key inside "/users" with own UID - pretty standard). I discovered in order to get the UID I need to subscribe to the af.auth observable and then inside there request the user data using af.auth.uid - which I've done

How to handle/queue multiple same http requests with RxJs?

半城伤御伤魂 提交于 2020-01-01 06:02:01
问题 I am making an Angular 4 app. Components are subscribing Observable. Sometimes Observable call the same url. For example 'refresh Access Token' if needed before any http request. But if I make different 3 http requests, it will "refresh if needed" 3 times the refresh token. So how to make only one http get to get a refreshed access token, and make the other observables 'wait' for the first one ? (I know 'wait' is not the good word for Observables). public get(url: string, options?: