In my Angular service, there is an object called _mapView
that gets initialized after the entire application and its dependencies are loaded. However, there is a possibility that users might interact with buttons or other elements triggering get-calls to this mapView object before the app is fully loaded, as the mapView loads asynchronously.
My aim is to ensure that if the object is not initialized yet, the program will wait until the mapView is ready.
@Injectable()
export class MapService {
private _mapViewPromiseResolveFx;
private _mapView: Promise<__esri.MapView> = new Promise(function(res, rej){
this._mapViewPromiseResolveFx = res;
}.bind(this)); // saving a reference to the resolve function allows the promise to be resolved externally
public resolveMapView(mapView: __esri.MapView) {
this._mapViewPromiseResolveFx(mapView);
}
public getMapView() {
return this._mapView;
}
}
At some point during the initialization of the AppComponent
, I initialize this variable:
self.mapService.resolveMapView(new MapView(mapViewProperties));
Therefore, whenever I require the mapView
, I use:
this.mapService.getMapView().then(function(mapView) {
Although this method appears to work, it feels like a somewhat clumsy approach and potential misuse of promises. Are there any other, more optimal solutions available?