1

I have the following code which works wonderfully. Unfortunately, each Observable is a heavy database query and they can't be executed in a parallel fashion.

import { Observable } from 'rxjs';

...

public search(criteria: SearchCriteria): void {
  this.searchAllTables(criteria).subscribe((responses: Array<JsonResponse>) => {
    this.progressDialogService.notifyComplete();
  });
}

private searchAllTables(criteria: SearchCriteria): Observable<Array<JsonResponse>> {
  return Observable.forkJoin(
    this.heavyDatabaseQuery1(criteria),
    this.heavyDatabaseQuery2(criteria),
    this.heavyDatabaseQuery3(criteria)
  );
}

private heavyDatabaseQuery1(criteria: SearchCriteria): Observable<JsonResponse> {
  return Observable.create(observer => {
    this.dbService.query1(criteria).subscribe((response: JsonResponse) => {
      observer.next(response);
      observer.complete();
    }, error => {
      observer.error(error);
    });
  });
}

... 

I'm having a very difficult time figuring out how to execute the heavy database queries sequentially. I've tried so many things (combinations of pipe, concat, flatMap, etc.) but none of them compile and I'm not sure it's worth detailing them all. Is this enough information for anyone to assist me in sequential execution of these methods? I would really appreciate it.

All I'm looking for is a means of replacing forkJoin with a function that executes sequentially rather than parallel. I need to notify another component when these queries are complete, and I'll need to do a bit of processing against the resulting array of json responses.

mmamane
  • 353
  • 4
  • 11

0 Answers0