प्रतीक्षा करने के लिए कैसे के लिए कई अनुरोध

0

सवाल

मैं कोशिश कर रहा हूँ बाहर आंकड़ा करने के लिए सबसे अच्छा तरीका क्या है के लिए प्रतीक्षा करने के लिए कई अनुरोधों और कुछ करते हैं जब वे कर रहे हैं, ध्यान में रखना है कि दोनों अनुरोध दोपहर के भोजन के एक ही समय में और वे इंतजार नहीं करना चाहिए एक दूसरे के लिए.

  // 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);
      });
    }
  }

वहाँ दो तरीके है कि कर रहे हैं कहा जाता है के अंदर ngoninit: fillTable() और fillTable2().

मैं करने की कोशिश की है करने के लिए निम्न विधि का उपयोग करने के लिए सुनो करने के लिए अनुमोदन और उनके लिए प्रतीक्षा करें

  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
  }

लेकिन के रूप में इसे बाहर मुड़ता है forkJoin नहीं कर सकते हैं सुनने के लिए सदस्यता है, वहाँ किसी भी अन्य तरीके से करने के लिए इसी तरह forkjoin सदस्यता के लिए?

वहाँ रहे हैं टो तरीकों में शामिल है कि HTTP अनुरोध और मैं इंतजार करना चाहते हैं दोनों के लिए नेटवर्क अनुरोध को पूरा करने के लिए और कुछ नहीं के रूप में जल्द ही वे समाप्त कर रहे हैं ।

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

सबसे अच्छा जवाब

1

मैं आमतौर पर कुछ घोषित बूलियन चर को हल करने के लिए इस प्रकार के अतुल्यकालिक समस्या है । आपके मामले में यह की तरह देखा है:

isSearch: boolean; isDeclarationSearch: boolean;

और फिर मैं initialized प्रत्येक चर के रूप में झूठी, और अंत में से प्रत्येक चर के मूल्य के रूप में सच है, और फिर मैं बनाने के लिए नया समारोह है कि काम जब दोनों के मूल्यों को सच कर रहे हैं.

2021-11-24 06:38:30

मुझे लगता है कि एकमात्र तरीका है यह करने के लिए मेरे मामले में
nevaehteekay
1

के बिंदु forkJoin है कि आप से सदस्यता नहीं है, यह अलग से, यह वापस आ जाएगी सभी परिणाम में से एक में जाने के लिए सदस्यता लें

let call: [] = [];

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

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

या

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

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

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

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में