我有一个正在调用的方法。方法完成并获得响应后,我想为变量分配值。目前,我不确定在设置值之前如何等待终结点调用完成。
这是我的代码
组件ngOninit
this._contentService.cacheDropdownOptions();
//在上述行完成后,我只能在下面继续
this.nationalities = JSON.parse(localStorage.getItem('nationality'));
this.titles = JSON.parse(localStorage.getItem('titles'));
this.idDocumentTypes = JSON.parse(localStorage.getItem('identityTypes'));
this.genders = JSON.parse(localStorage.getItem('genders'));
除了使用setTimout之外,还有其他方法吗?
public cacheDropdownOptions() {
this._subscriptions.push(this.getRoles().subscribe((resp) => {
if (resp) {
const rolesArr = Object.entries(resp).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('roles', JSON.stringify(rolesArr));
} else {
this._errorService.openErrorPopup('An error occured while extracting the roles.');
}
}, (error) => {
this._errorService.openErrorPopup('An error occured while extracting the roles.');
}));
const medicalPlansArr = medicalAidPlans.map(x => ({ label: x.name, value: x.id }));
this._subscriptions.push(this.getLookups().subscribe((resp) => {
if (resp) {
localStorage.setItem('medicalPlans', JSON.stringify(medicalPlansArr));
if (resp.genders) {
const genderArr = Object.entries(resp.genders).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('genders', JSON.stringify(genderArr));
}
if (resp.races) {
const racesArr = Object.entries(resp.races).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('races', JSON.stringify(racesArr));
}
if (resp.titles) {
const titlesArr = Object.entries(resp.titles).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('titles', JSON.stringify(titlesArr));
}
if (resp.maritalStatus) {
const maritalStatusArr = Object.entries(resp.maritalStatus).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('maritalStatus', JSON.stringify(maritalStatusArr));
}
if (resp.nationality) {
const nationalityArr = Object.entries(resp.nationality).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('nationality', JSON.stringify(nationalityArr));
}
if (resp.identityTypes) {
const identityTypesArr = Object.entries(resp.identityTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('identityTypes', JSON.stringify(identityTypesArr));
}
if (resp.companyTypes) {
const companyTypeArray = Object.entries(resp.companyTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('companyTypes', JSON.stringify(companyTypeArray));
}
if (resp.industries) {
const industryArray = Object.entries(resp.industries).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('industry', JSON.stringify(industryArray));
}
if (resp.fileTypes) {
const fileTypesArr = Object.entries(resp.fileTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('fileTypes', JSON.stringify(fileTypesArr));
}
if (resp.userDocumentCategories) {
const documentCategoriesArray = Object.entries(resp.userDocumentCategories).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('documentCategories', JSON.stringify(documentCategoriesArray));
}
if (resp.reasonExtend) {
const reasonExtendArr = Object.entries(resp.reasonExtend).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('extendReasons', JSON.stringify(reasonExtendArr));
}
if (resp.reasonRescind) {
const reasonRescindArr = Object.entries(resp.reasonRescind).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('rescindReasons', JSON.stringify(reasonRescindArr));
}
if (resp.sources) {
const savingsSourcesArr = Object.entries(resp.sources).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('savingsSources', JSON.stringify(savingsSourcesArr));
}
localStorage.setItem('contentSet', 'true');
this.lookupsCached.next(true);
} else {
this._errorService.openErrorPopup('An error occured while extracting the lookups.');
}
}, (error) => {
this._errorService.openErrorPopup('An error occured while extracting the lookups.');
}));
}
答案 0 :(得分:1)
您可以在async / await中使用基本的Promise。
Server > Options file > InnoDB