N
N
nasnetstep2017-03-07 13:02:47
JavaScript
nasnetstep, 2017-03-07 13:02:47

Rxjs how to make two http requests in sequence?

There is a stream for receiving data and deleting data.
It is necessary to sequentially delete the data, first the first data, and only then the second. But first you need to make sure they are.
Here's what I wrote, but I think it's wrong

let params1 = 1;
let params2 = 2;

service.getData(): Observable
service.deleteData() : Observable

service.getData(params1).subscribe(res => {
  if(res) {
     service.deleteData(params1).subscribe(res => {
       service.getData(params2).subscribe(res => {
           if(res) {
             service.deleteData(params2).subscribe();
           }
     });
  } else {
     service.getData(params2).subscribe(res => {
        if(res) {
           service.deleteData(params2).subscribe();
        }
  }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nasnetstep, 2017-03-10
@nasnetstep

In general, I found the answer myself, maybe it will be useful to someone

// последовательно

let someStream$ = service.getData().flatMap(res => {
  return service.deleteData(res)
})

someStream$.subscribe(res => {
   // res - результат последнего 
})

// параллельно
let getData$ = service.getData();
let deleteData$ = service.deleteData();

Observable.forkJoin([getData$, deleteData$]).subscribe(results => {
      // results[0] дата из getData$
      // results[1] дата из deleteData$
      this.a = results[1];
      this.b = results[0];
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question