0101adm
0101adm

Reputation: 119

RxJS use response only from last promise

how can i only use the last promise created, canceling (or not using) all prior promises?

the function can be ran an unlimited amount of times.

rx.Observable.fromPromise(listingFactory.list())
            .flatMapLatest((e: any) => {
                return e;
            })
            .subscribe((e: any) => {
                console.log(e);
            });

Upvotes: 2

Views: 550

Answers (1)

user3743222
user3743222

Reputation: 18665

if your listingFactory.list() returns an observable of promises, then you could write :

listingFactory.list()
        .flatMapLatest((promise: any) => {
         return promise;
         })
        .subscribe((resolved_value: any) => {                     
          console.log(resolved_value);
        }, (error: any) => {
            console.log(error)});

flatMapLatest will wait for the latest emitted promise to resolve, discarding the previous promises. Note that those promises cannot be 'cancelled', but their resolved value (or rejection) will be ignored. Note also that flatMapLatest allows you to return an observable, promise, or iterable. In that particular case, we can then just return the promise directly without necessity to use .fromPromise operator. Also, the fromPromise operator expects only one promise as argument, not a list of them.

So what is the type of listingFactory.list()? How do you create your source of promises? Did I misunderstand your intent?

Upvotes: 1

Related Questions