I am trying to figure out what is the best way to wait for multiple requests and do something when they are done, keep in mind that both request lunch at the same time and they shouldn't wait for each other.
// 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);
});
}
}
There are two methods that are called inside ngoninit: fillTable() and fillTable2().
I have tried to use the following method to listen to subscriptions and wait for them
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
}
But as it turns out forkJoin can't listen to subscriptions, is there any other way similar to forkjoin for subscriptions?
There are tow methods that include HTTP requests and I want to wait for both network requests to complete and do something as soon at they are finished.
CodePudding user response:
I usually declare some boolean variables to solve this type of asynchronous problem. In your case it's looked like:
isSearch: boolean; isDeclarationSearch: boolean;
And then I initialized each variable as false, and in the end of each variable I change the value as true, and then I create new function that work when both of values are true.
CodePudding user response:
The point of forkJoin is that you would subscribe from it, not separately, it will return all the result in one go in the subscribe
let call: [] = [];
call.push(this.declarationsService.getDeclarations1()); call.push(this.declarationsService.getDeclarations2()); call.push(this.declarationsService.getDeclarations3());
forkJoin(call).subscribe((res) => { });
or
forkJoin({
'prop1' :this.declarationsService.getDeclarations1()
'prop2' : this.declarationsService.getDeclarations2()
}).subscribe((result) => { result.prop1; result.prop2 });