Skip to content Skip to sidebar Skip to footer

Why Is Rxjs Not Canceling My Promise?

I am using rxjs in my angular app. I don't always get the data back in the order I want when multiple REST calls are made. Controller: constructor() { rx.Observable.fromPromis

Solution 1:

Since you have done nothing to "link" the two sequences, how can RxJs ever know they are related?

You need to create a single stream, then you can use switch to observe the most recent request only:

constructor() {
    this._requests = new Rx.Subject();

    // listen to requests
    this._requests.switch().subscribe(res => this.setExpenses(res));

    // start the first request
    this._requests.onNext(this.getExpenses(toDate, fromDate));
}

updateDate() {
    // start a new request
    this._requests.onNext(this.getExpenses(toDate, fromDate));
}

Note that this won't actually cancel the previous requests. It will just ignore any results they produce. If you really want to cancel them, your expense service would need to provide an API for cancelling in-progress requests.


Post a Comment for "Why Is Rxjs Not Canceling My Promise?"