I am having an issue with race conditions inside of my angular app.
I figure I can solve this by awaiting the get request whose response the second get request depends upon.
That being said I have been having trouble making the controller function async, when I define an async function and pass it in as the controller function – the page does not load at all.
Here is a basic view of the app with the single controller
angular.module('myApp', [
'ngRoute',
'myApp.home',
'angularMoment'
]).
constant("ROOT_API","https://" + window.location.host + shortpath + "/").
controller('NavigationController', navCtrlCall).
config(['$routeProvider', function($routeProvider) {
$routeProvider.otherwise({redirectTo: '/'});
}])
Here is the controller function
async function navCtrlCall($scope, $http, ROOT_API, $location, $window){
async function setValues(){
$scope.$location = $location;
$scope.root = window.location.href.replace(window.location.hash, '');
$scope.loggedIn = sessionStorage.getItem("uid") != null;
var appId = await $http.get(ROOT_API+"webapi/public/admin/attribute/websiteAppId").toPromise();
$scope.appId = appId.data.replace(new RegExp(/^"/), "").replace(new RegExp(/"$/), "");
}
await setValues();
}
$scope.appId is the variable that i need set for the second get request to work, otherwise it passes in 'undefined'.
The second controller that makes the next get request
'use strict';
angular.module('myApp.home', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'modules/home/home.html',
controller: 'HomeCtrl'
});
}])
.controller('HomeCtrl', ["$scope", "$http", "ROOT_API", function($scope,$http,ROOT_API) {
var today = new Date();
var milisec = today.getTime();
$http.get(ROOT_API+"webapi/public/events/" + $scope.appId, {
params: {
fromDate: milisec
}
}).success(
function(data){
$scope.events = data.slice(0,5);
}
);
}]);