I have been working on an Angular component where I need to perform some actions after a data service method returns some data asynchronously. Although I attempted to use async/await in my code, I feel that I may not have fully understood how it works. Here is the method I have implemented in the component:
async redrawGrid(params: any) {
let v;
try {
v = await this.innerGridService.loadAccountList()
} catch (error) {
// handle error
}
params.node.childFlower.setRowHeight((this.globalRowCount * 46) + 48);
this.gridOptions.api.onRowHeightChanged();
}
The loadAccountList() method in my service returns an observable:
...
@Injectable()
export class ClientListService {
...
responseObject$: Observable<ResponseObject>;
...
}
loadAccountList() {
this.logService.info('InnerGridService loadAccountList()');
if (this.dataStore.responseObject) {
this.refreshDataStore();
}
let token = this.authenticationService.getToken();
let headers = new Headers({ 'netx.esf.AuthenticationService': token });
let options = new RequestOptions({ headers: headers });
this.http.get(`${this.baseUrl}`, options)
.map((response: Response) => response.json())
.subscribe(
(data: InnerGridResponse) => {
this.formatData(data.response.accountlist);
this.dataStore.InnerGridResponse = data;
this.dataStore.responseObject = data.response;
this._InnerGridResponse.next(Object.assign({}, this.dataStore).InnerGridResponse);
this._responseObject.next(Object.assign({}, this.dataStore).responseObject);
if (data.errorCode === 0 || data.errorCode === 90) { // success
this.logService.info('loadAccountList() success: ', data.errorCode);
this.clearError();
} else { // error code from backend
this.logService.error('loadAccountList() errorCode: ', data.errorCode);
this.setError(this.message[0], data.errorCode);
}
},
error => { // exception
this.logService.error('loadAccountList() exception:', error);
this.setError(this.message[0], error._body);
// throw error;
});
return this.responseObject$;
}