Jak se dočkat, pro více žádostí

0

Otázka

Snažím se zjistit, co je nejlepší způsob, jak se dočkat, pro více žádostí a něco udělat, když jsou hotové, mějte na paměti, že obě žádosti oběd ve stejnou dobu a neměli by čekat na sebe.

  // 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í dvě metody, které jsou volány uvnitř ngoninit: fillTable() a fillTable2().

Snažil jsem se použít následující metodu poslouchat odběry a čekat na ně

  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 jak se ukázalo forkJoin nemůžu poslouchat odběry, je nějaký jiný způsob, jak podobné forkjoin pro odběry?

Tam jsou tažné metody, které zahrnují požadavky HTTP a chci počkat, jak pro síťové požadavky na kompletní a udělat něco hned, na, jsou hotové.

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

Nejlepší odpověď

1

Já obvykle deklarovat některé booleovské proměnné, jak vyřešit tento typ asynchronní problém. Ve vašem případě to vypadalo:

isSearch: boolean; isDeclarationSearch: boolean;

A pak jsem inicializovat jednotlivé proměnné jako nepravdivé, a na konci každé proměnné změnit hodnotu jako pravda, a pak jsem se vytvořit nové funkce, které pracují, když obě hodnoty jsou pravda.

2021-11-24 06:38:30

Myslím, že to je jediný způsob, jak to udělat v mém případě
nevaehteekay
1

Bod forkJoin je, že byste se přihlásit z něj, a ne odděleně, vrátí celý výsledek na jeden zátah ve přihlásit

let call: [] = [];

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

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

nebo

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

V jiných jazycích

Tato stránka je v jiných jazycích

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