rxjs

How can I use RxJs to hold off any requests for an AJAX call until the previous one resolves

非 Y 不嫁゛ 提交于 2019-12-19 06:45:32
问题 I have an observable that represents an action that is triggered by some outside component. For the purpose of this question, let's call it createBananaAction . I have a bananaService with a method create that performs an AJAX request and returns the created banana as a Promise . So, whenever some data arrives from the createBananaAction , we want to call bananaService.create() . The code looks like this: (using RxJs) this.createdBananas = createBananaAction.flatMap(() => bananaService.create

How can I use RxJs to hold off any requests for an AJAX call until the previous one resolves

血红的双手。 提交于 2019-12-19 06:43:46
问题 I have an observable that represents an action that is triggered by some outside component. For the purpose of this question, let's call it createBananaAction . I have a bananaService with a method create that performs an AJAX request and returns the created banana as a Promise . So, whenever some data arrives from the createBananaAction , we want to call bananaService.create() . The code looks like this: (using RxJs) this.createdBananas = createBananaAction.flatMap(() => bananaService.create

What does subscribe do, and how it is related to Observable?

青春壹個敷衍的年華 提交于 2019-12-19 05:01:23
问题 I'm new to Angular and the tutorial I followed has the term " Observable ". The tutor explained it, but I didn't completely understand. What is an Observable , and why do we always have to call observable.subscribe() ? What does subscribe() actually do? 回答1: What is an Observable ? An Observable can be seen as a data source. That data might exist (or not) and might change over time (or not). An Observable emits data, until it has nothing to emit anymore and then completes (there are some

Angular - Correctly using RXJS expand operator to make recursive http calls

柔情痞子 提交于 2019-12-19 04:24:20
问题 I am attempting to make recursive http calls to Reddit's API using a value from the previous call. The problem is that the previous call is not finished before the next one starts, so duplicate calls are being made. The "after" value should be updated for every call until the "after" value is undefined. I found this related post and have attempted to use the solution described, but I can't figure out how to make sure the previous call is finished before making the next call. Below is my

RxJS5 emit array items over time and repeat forever

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 04:05:50
问题 I want to emit array items over time (a one second interval between each emit) and when all items have been emitted, repeat over and over. I know how to do this, but I want to know if there is something more succinct than .. const MY_ARRAY = ['one','two','three']; const item$ = Rx.Observable.interval(1000).take(MY_ARRAY.length).repeat().map(x => MY_ARRAY[x]); item$.subscribe(x => console.log(x)); thanks output is .. "one" "two" "three" "one" "two" "three" etc EDIT: ATOW, the answers here are

Passing composite data in rxjs observable chains

孤街醉人 提交于 2019-12-19 02:06:31
问题 I have a block of code where I'm calling observables in a chain like so: getData().flatMap(results => { return callNextDataMethod(results); } .flatMap(results2 => { // next operation and so forth }) Now, I understand that flatMap will allow me to pass the results of the previous observable to the next one. However what I need is to both do that as well as pass the results on the first. Let's assume that I do some cleanup, validation, etc on the data that comes back in getData and I want that

RXJS control observable invocation

穿精又带淫゛_ 提交于 2019-12-18 18:59:27
问题 I use RxJs version 5 within my Angular 2 project. I want to create some observables but I don't want the observables being invoked immediately. In version 4 you could control the invocation with (for example) the Controlled command or Pausable Buffers. But that functionality is not (yet) available in version 5. How can I get the this kind of functionality in RxJs 5? My ultimate goal is to queue the created observables and invoke them one by one. The next one is only invoked when the previous

RXJS control observable invocation

元气小坏坏 提交于 2019-12-18 18:59:27
问题 I use RxJs version 5 within my Angular 2 project. I want to create some observables but I don't want the observables being invoked immediately. In version 4 you could control the invocation with (for example) the Controlled command or Pausable Buffers. But that functionality is not (yet) available in version 5. How can I get the this kind of functionality in RxJs 5? My ultimate goal is to queue the created observables and invoke them one by one. The next one is only invoked when the previous

Angular 2. How to handle 4xx errors with redirect in Observable?

China☆狼群 提交于 2019-12-18 16:45:12
问题 I have a service that calls an api getItems(itemId: number): Observable<any> { return this.http.get(url, headers) .map(this.extractDataSingle) .catch(this.handleError) } If the server responds with an 4xx the catch part is called. Here is my handleError method. private handleError = (error: any) => { //Here I want to redirect to login. } I want to redirect to the login page. Simply typing this._router.navigate(['Login']); does not work since I have to return a Observable . Returning an empty

How to interleave streams (with backpressure)

百般思念 提交于 2019-12-18 15:18:27
问题 Suppose I have two possibly infinite streams: s1 = a..b..c..d..e... s2 = 1.2.3.4.5.6.7... I want to merge the streams and then map merged stream with slowish asynchronous operation (e.g. in Bacon with fromPromise and flatMapConcat ). I can combine them with merge : me = a12b3.c45d6.7e... And then map s1 = a..b..c..d..e... s2 = 1.2.3.4.5.6.7... me = a12b3.c45d6.7e... mm = a..1..2..b..3..c..4..5.. As you see greedier s2 streams gets advantage in the long run. This is undesired behaviour . The