简体   繁体   中英

Synchronous calls

I want to delete the file after i downloaded it from the server, but the delete method in service executes first even if i place it below the line of download file this is my code:

generateClearanceExcel(): void {
    if (this.selectedIds.length > 0) {
        var departureDate = prompt("Please enter departure date.", "");
        if (departureDate !== null && departureDate !== "") {
            this.generating = true;
            this.clearanceService.generateClearanceExcel(this.selectedIds, departureDate).subscribe(
                result => {
                    this.generating = false;
                    window.location.href = "clearance/downloadreport/?fileName=" + result.fileName;
                    this.clearanceService.deleteFile(result.fileName).subscribe();

                    if (result.success == true) {
                        if (result.infos.length > 0) {
                            this.alertService.info(result.infos);
                        }
                    }
                    else {
                        this.alertService.error(result.errors);
                    }
                }
            );
        }
    }
}

TIA.

Lets try with timeout

generateClearanceExcel(): void {
    if (this.selectedIds.length > 0) {
        var departureDate = prompt("Please enter departure date.", "");
        if (departureDate !== null && departureDate !== "") {
            this.generating = true;
            this.clearanceService.generateClearanceExcel(this.selectedIds, departureDate).subscribe(
                result => {
                    this.generating = false;
                    window.location.href = "clearance/downloadreport/?fileName=" + result.fileName;
                     setTimeout(() => {
                      this.clearanceService.deleteFile(result.fileName).subscribe();
                    }, 3000);
                    setTimeout(function(){  }, 3000);                       

                    if (result.success == true) {
                        if (result.infos.length > 0) {
                            this.alertService.info(result.infos);
                        }
                    }
                    else {
                        this.alertService.error(result.errors);
                    }
                }
            );
        }
    }
}

You should place the delete method inside the condition of success of download.

    generateClearanceExcel(): void {
        if (this.selectedIds.length > 0) {
            var departureDate = prompt("Please enter departure date.", "");
            if (departureDate !== null && departureDate !== "") {
                this.generating = true;
                this.clearanceService.generateClearanceExcel(this.selectedIds, departureDate).subscribe(
                    result => {
                        this.generating = false;
                        window.location.href = "clearance/downloadreport/?fileName=" + result.fileName;                   

                        if (result.success == true) {
                        this.clearanceService.deleteFile(result.fileName).subscribe();
                        if (result.infos.length > 0) {
                            this.alertService.info(result.infos);
                        }
                    }
                    else {
                        this.alertService.error(result.errors);
                    }
                }
            );
        }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM