Ako čakať na viacerých žiadostí

0

Otázka

Snažím sa prísť na to, čo je najlepší spôsob, ako čakať na viacerých žiadostí a robiť niečo, keď sú vykonané, majte na pamäti, že obe žiadosti obed v rovnakom čase a nemali by čakať na seba.

  // table filling
  public SUB_getActSearch: Subscription;


  // table filling 2
  public SUB_getDeclarationSearch: Subscription;

  public fillTable(): void {
    const searchOBJ = {
      CreateDateFrom: this.currentDate,
      CreateDateTo: this.currentDate,
      ActNumber: this.ActNumber == undefined ? '' : this.ActNumber,
      vinCode: this.vinCode == undefined ? '' : this.vinCode,
      GovNumber: this.GovNumber == undefined ? '' : this.GovNumber,
      IsFromDashboard: true
    };
    const searchOBJ2 = {
      ActNumber: this.ActNumber == undefined ? '' : this.ActNumber,
      vinCode: this.vinCode == undefined ? '' : this.vinCode,
      GovNumber: this.GovNumber == undefined ? '' : this.GovNumber,
    };
    if ((this.ActNumber && this.ActNumber != undefined && this.ActNumber != null) || (this.vinCode && this.vinCode != undefined && this.vinCode != null) || (this.GovNumber && this.GovNumber != undefined && this.GovNumber != null)) {
      this.SUB_getActSearch = this.searchService.getActSearch({ ...searchOBJ2, PageSize: this.pageSize, PageNumber: this.currentPage }).subscribe((res) => {
        this.Act_Data = [];
        let headerPagin = JSON.parse(res.headers.get('X-Pagination'));
        this.elementsLength = headerPagin.totalCount;
        this.currentPage = headerPagin.currentPage;
        this.pageSize = headerPagin.pageSize;
        res.body.map((param) => {
          this.Act_Data.push(param);
        });
        this.ActDataSource = new MatTableDataSource<Element>(this.Act_Data);
        // console.log('Your form data : ', this.ActDataSource);
      });
    } else {
      this.SUB_getActSearch = this.searchService.getActSearch({ ...searchOBJ, PageSize: this.pageSize, PageNumber: this.currentPage }).subscribe((res) => {
        this.Act_Data = [];
        let headerPagin = JSON.parse(res.headers.get('X-Pagination'));
        this.elementsLength = headerPagin.totalCount;
        this.currentPage = headerPagin.currentPage;
        this.pageSize = headerPagin.pageSize;
        res.body.map((param) => {
          this.Act_Data.push(param);
        });
        this.ActDataSource = new MatTableDataSource<Element>(this.Act_Data);
        // console.log('Your form data : ', this.ActDataSource);
      });
    }
  }


  public fillTable2(): void {
    const searchOBJ = {
      DeclarationCreateDateFrom: this.currentDate.substring(0, 10),
      DeclarationCreateDateTo: this.currentDate,
      DeclarationNumber: this.ActNumber == undefined ? '' : this.ActNumber,
      VinCode: this.vinCode == undefined ? '' : this.vinCode,
      TransitNumber: this.GovNumber == undefined ? '' : this.GovNumber,
    };
    const searchOBJ2 = {
      DeclarationNumber: this.ActNumber == undefined ? '' : this.ActNumber,
      VinCode: this.vinCode == undefined ? '' : this.vinCode,
      TransitNumber: this.GovNumber == undefined ? '' : this.GovNumber,
    };
    if ((this.ActNumber && this.ActNumber != undefined && this.ActNumber != null) || (this.vinCode && this.vinCode != undefined && this.vinCode != null) || (this.GovNumber && this.GovNumber != undefined && this.GovNumber != null)) {
      this.SUB_getDeclarationSearch = this.declarationsService.getDeclarations({ ...searchOBJ, IsFromDashboard: true, PageSize: this.pageSizeDecl, PageNumber: this.currentPageDecl }).subscribe((res) => {
        this.Declaration_Data = [];
        let headerPaginDecl = JSON.parse(res.headers.get('X-Pagination'));
        this.elementsLengthDecl = headerPaginDecl.totalCount;
        this.currentPageDecl = headerPaginDecl.currentPage;
        this.pageSizeDecl = headerPaginDecl.pageSize;
        res.body.map((param) => {
          this.Declaration_Data.push(param);
        });
        this.DeclarationDataSource = new MatTableDataSource<Element>(this.Declaration_Data);
        // console.log('Your form data : ', this.DeclarationDataSource);
      });
    } else {
      this.SUB_getDeclarationSearch = this.declarationsService.getDeclarations({ ...searchOBJ, IsFromDashboard: true, PageSize: this.pageSizeDecl, PageNumber: this.currentPageDecl }).subscribe((res) => {
        this.Declaration_Data = [];
        let headerPaginDecl = JSON.parse(res.headers.get('X-Pagination'));
        this.elementsLengthDecl = headerPaginDecl.totalCount;
        this.currentPageDecl = headerPaginDecl.currentPage;
        this.pageSizeDecl = headerPaginDecl.pageSize;
        res.body.map((param) => {
          this.Declaration_Data.push(param);
        });
        this.DeclarationDataSource = new MatTableDataSource<Element>(this.Declaration_Data);
        // console.log('Your form data : ', this.DeclarationDataSource);
      });
    }
  }

Existujú dve metódy, ktoré sa nazývajú vnútri ngoninit: fillTable() a fillTable2().

Snažil som sa použiť nasledujúci postup počúvať predplatné a čakať na ich

  public testingforkJoin(): void {
    forkJoin([this.SUB_getActSearch, this.SUB_getDeclarationSearch]
    ).subscribe(val => 
console.log("xxxxx", val));
// call some method here as soon as both are done
  }

Ale ako sa ukázalo forkJoin nemôže počúvať predplatné, existuje nejaký iný spôsob, ako podobné forkjoin na predplatné?

Tam sú ťažné metódy, ktoré zahŕňajú HTTP požiadavky a chcem čakať na obe siete žiadosti na dokončenie a robiť niečo, čo v sú hotové.

angular asynchronous fork-join subscribe
2021-11-24 06:11:59
2

Najlepšiu odpoveď

1

Ja zvyčajne vyhlásiť niektoré booleovské premenné vyriešiť tento typ asynchrónne problém. Vo vašom prípade je to vyzeralo:

isSearch: boolean; isDeclarationSearch: boolean;

A potom som inicializovaný každej premennej ako nepravdivé, a na konci každej premennej I hodnotu zmeniť tak, ako pravda, a potom som vytvoriť nové funkcie, ktoré pracujú keď obe hodnoty sú pravdivé.

2021-11-24 06:38:30

Myslím, že to je jediný spôsob, ako robiť to v mojom prípade
nevaehteekay
1

Miesto forkJoin je, že by ste si vyžiadať od neho, nie samostatne, vráti všetky výsledok v jednom ísť prihlásiť sa na odber

let call: [] = [];

call.push(this.declarationsService.getDeclarations1()); call.push(this.declarationsService.getDeclarations2()); call.push(this.declarationsService.getDeclarations3());

forkJoin(call).subscribe((res) => {  }); 

alebo

 forkJoin({
 'prop1' :this.declarationsService.getDeclarations1()
 'prop2' : this.declarationsService.getDeclarations2()
}).subscribe((result) => { result.prop1; result.prop2   }); 
2021-11-24 06:41:33

V iných jazykoch

Táto stránka je v iných jazykoch

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................