My angular service holds an object _mapView
which is initialized after the whole application with all its components and dependencies has been loaded. However, the user might already press buttons etc. which result in get-calls to this mapView object before the app is fully loaded, because the mapView is loaded asynchronously.
My goal is that if the object hasn't been initialized yet, the programm will resume when the mapView is initialized.
@Injectable()
export class MapService {
private _mapViewPromiseResolveFx;
private _mapView: Promise<__esri.MapView> = new Promise(function(res, rej){
this._mapViewPromiseResolveFx = res;
}.bind(this)); // save a reference to the resolve function so the promise can be resolved from outside its function scope
public resolveMapView(mapView: __esri.MapView) {
this._mapViewPromiseResolveFx(mapView);
}
public getMapView() {
return this._mapView;
}
}
Somewhen during initialization of the AppComponent
, this variable is initialized:
self.mapService.resolveMapView(new MapView(mapViewProperties));
So, whenever I need the mapView
, I do:
this.mapService.getMapView().then(function(mapView) {
This seems to be working, but feels like very "hacky" approach and misuse of promises. What other, better options do I have?