В этом случае я бы предложил использовать async/await
для решения ваших задач:
// Add `async` here
async your_function_name () {
...
let report;
if (!this.userInfo) {
// Waits until your user info is loaded
this.userInfo = await this.nrcService.getUserInfo();
}
report = {
ReportType: reportType,
ReportID: id,
ReportFormat: format,
ReportName: `${reportType}_${id}`,
Filters: shouldNotParseFilters ? filterContent : [],
ViewFields: columns || [],
OrgName: this.userInfo[0].orgname,
FullName: this.userInfo[0].fullname
} as SavedReport;
if (!shouldNotParseFilters) {
this.reportFilterService.mapReportFilters(<ReportFilterContext>filterContent, report);
}
report.Filters.push({ 'maxitems': [-1] });
this.nrcService.downloadReport(report, fileName).subscribe({ error: err => this.nrcService.handleError(err) });
}
Если вы не знакомы с async/await
, см. здесь ; -)
Другой вариант - использовать Обещания без async/await
, но это выглядит не так хорошо:
let report;
// Create a new promise, which resolves directly
let promise = new Promise(r => r());
if (!this.userInfo) {
// Assigns the loading promise to the promise variable
promise = this.nrcService.getUserInfo().then(data => {
this.userInfo = data;
});
}
// Waits until the promise is resolved
promise.then(() => {
report = {
ReportType: reportType,
ReportID: id,
ReportFormat: format,
ReportName: `${reportType}_${id}`,
Filters: shouldNotParseFilters ? filterContent : [],
ViewFields: columns || [],
OrgName: this.userInfo[0].orgname,
FullName: this.userInfo[0].fullname
} as SavedReport;
if (!shouldNotParseFilters) {
this.reportFilterService.mapReportFilters(<ReportFilterContext>filterContent, report);
}
report.Filters.push({ 'maxitems': [-1] });
this.nrcService.downloadReport(report, fileName).subscribe({ error: err => this.nrcService.handleError(err) });
});