rxjs

Observable - converting 2 promises into an observable

徘徊边缘 提交于 2019-12-12 02:39:17
问题 I have a popular scenario where I need to create one promise that returns data which is fed to a second promise. If the first promise fails, I need to cancel the second promise. In 'Promise' land it would look something like this: Fn1.doPromise( initialData ) .then(info => { Fn2.doPromise( info ) .then(result => { //success - return result }) .catch(error => { //error }); }) .catch(error => { //cancel 2nd promise and show error }); Now I am trying to learn the best way to do this using

RxJs Observable interval until reached desired value

戏子无情 提交于 2019-12-12 02:30:36
问题 I want to poll for changes and when a desired value is reached the Observable should complete (or wait until timeout). Right now I use the filter which works fine to wait until the desired value is reached. But I want the Observable to push events while waiting for this value. For example, I wait for the status 'success' and until the status changes to 'success' the status 'testing' is returned from my service. But since the filter is waiting for 'success', 'testing' never returns. My code

combine RxJs Observable array

北城余情 提交于 2019-12-12 02:29:27
问题 I'm trying to do infinite-scroll in a Ionic2+Meteor mobile app which lists the contacts. In the following code, findContacts() function returns 10 contacts at a time. contacts: Observable; findContacts() :Observable<Contact[]> { /* logic to pull Contacts is here ...*/ } this.contactsSub = MeteorObservable.subscribe('contacts', options).subscribe(() => { MeteorObservable.autorun().subscribe(() => { if(!this.contacts) { this.contacts = this.findContacts(); } }); }); This code gets called every

Rxjs - Subscribing to interdependent observables

微笑、不失礼 提交于 2019-12-12 02:22:06
问题 I am learning angular 2 and rxjs. I have 3 variables, A B C. B depends on the value of A C depends on the value of A and B I am trying to setup up the observables such that: When A is updated, B and C will be auto updated. When B is updated, C will be auto updated. I tried two setups but they are not satisfactory. First setup: B subscribes observable A; C subscribes observable B withlatestfrom A. The changes in A did cascade down to B then to C but the value from A is not the latest. Second

Rx Subscribe OnComplete fires but cannot use the data

北城以北 提交于 2019-12-12 02:12:45
问题 I have a product service with a method that returns an Observable from json data. product.service.ts.... getProducts(): Observable<IProductData[]> { return this._http.get(this._productUrl) .map((response: Response) => <IProductData[]> response.json()) .catch(this.handleError); } In my component I subscribe to the serivce and call the getProducts() method returning the Observable IProductData[]. mycomponent.ts .. productData: IProductData[]; ngOnInit(): void { this._productService

CORS ISSUE in Angular2 [duplicate]

梦想与她 提交于 2019-12-12 02:08:43
问题 This question already has answers here : Origin is not allowed by Access-Control-Allow-Origin (18 answers) Closed 3 years ago . I am using webpack angular2 starter kit (https://github.com/AngularClass/angular2-webpack-starter) for my application and now i have an issue. I try to make get call getSomeData(): Observable<any> { let url = here is my api URL; let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8', "Access-Control-Allow-Origin": "*", 'dataType': 'json', });

RxJS Observable & Observer issues

亡梦爱人 提交于 2019-12-12 01:44:16
问题 I have an Angular 2 service that executes a few steps to authenticate and log in an app user. Any time I try to call next() on my Observer I get an error that it is undefined. The only place I can successfully call next() on it is inside contructor when the Observable is instantiated. If I call authenticateUser() I get an error that this.isLoggedIn is undefined. AuthService.ts public isLoggedIn$: Observable<boolean>; private isLoggedIn: Observer<boolean>; constructor(...) { this.isLoggedIn$ =

Angular 2 reSTARTable timer

故事扮演 提交于 2019-12-12 01:29:22
问题 I've looked everywhere, but I just can't find a solution on how to create a timer with Observable, or SimpleTimer, which I can restart from a specific value. I tried unsubscribing, and then subscribing, but it won't start from the beginning, and neither can I use this.timer = new Observable.timer(...); Can anyone help me with this, please? Thanks! 回答1: You can achieve re-startable timer for example by using switchMap() : const timerControl$ = new Subject<any>(); const timer$ = timerControl$

How to return additional values from flatMap in RxJs

喜夏-厌秋 提交于 2019-12-12 01:02:39
问题 I need to return an additional value other than Observable, key string from flatMap from below setup: this.subscribeInit = this.tradingService.getInProgress() .pipe(flatMap((s) => { this.keys = s.map((tdi) => tdi.key); return this.keys; }), flatMap((key) =>{ var res = this.tradingService.getTradingData(key);//returns `Observable<any>` return res; })) .subscribe(res => { console.log('Key is : ' + res.key); }, undefined, () => this.onComplete()); Something like: flatMap((key) =>{ var res = this

Rx.js and application workflow

无人久伴 提交于 2019-12-12 00:29:31
问题 I've got a web application where I'm using the Rx.js for handling event streams. The app uses a rest api provided by a backend. Mostly I make a subscription for an api call and when a request is done I render results and reset other controls states (hide progress elements and so on). Some api calls can be failed when an auth token is expired and I have to make a user to login again (show a login popup or so). I'm curious is there a way to "restore" an api call stream after a successful login?