'Promise' is undefined error when run on IE

Rahul Kalidindi picture Rahul Kalidindi · Jun 28, 2016 · Viewed 10.6k times · Source

I have a controller code that runs fine on Chrome, but when run on IE 10 the same code returns

ReferenceError: 'Promise' is undefined

The function that returns the error is:

new Promise(function(resolve) {
    MetaModel.load($scope, (regionExist ? reqParmRegion[1] : reqParmRegion), (screenExist ? reqParmScreen[1] : reqParmScreen), resolve);
}).then(function(){

    loadRelationshipByStep($scope.preStep);
     if($rootScope.regionId === 'us') {
        $rootScope.currRel = 'itself';
    } 

    if($rootScope.screenId.indexOf('search') !== -1 ){
       EnumerationService.loadEnumerationByTab();
    }  
    // load data for tab click
    if($rootScope.currRel !== 'undefined' && $rootScope.currRel !== 'itself' && $scope.regionId !== 'us'){
        $scope.loadDataByTab($rootScope.currRel);
    } else if($rootScope.resourceHref !== undefined) {
        var params = {};
         resourceFactory.get($rootScope.resourceHref, params, $rootScope.headers).success(function(responseData){
            var data = responseData.data || responseData;
            if (data) {
                $scope.data=data;
                EnumerationService.executeEnumerationFromBackEnd(data, 'create');
                if($rootScope.regionId === 'us'){
                    EnumerationService.executeEnumerationFromBackEnd(data, 'fetch');    
                }
            }
        });            
    }
});

Do i need to add any $promise variables?

Answer

detaylor picture detaylor · Jun 28, 2016

IE does not support native the native javascript Promise. See browser compatibility on MDN.

Angular includes the $q service that provides promise functionality. You can create a deferred object using $q.defer() and return the promise from that object.

I think that the equivalent code would be:

// create the deferred object
var deferred = $q.defer();

// pass the resolve method as the callback
MetaModel.load($scope, (regionExist ? reqParmRegion[1] : reqParmRegion), 
    (screenExist ? reqParmScreen[1] : reqParmScreen), deferred.resolve);

// chain actions onto the promise.
deferred.promise.then(function(){

    loadRelationshipByStep($scope.preStep);
     if($rootScope.regionId === 'us') {
        $rootScope.currRel = 'itself';
    } 

    if($rootScope.screenId.indexOf('search') !== -1 ){
       EnumerationService.loadEnumerationByTab();
    }  
    // load data for tab click
    if($rootScope.currRel !== 'undefined' && $rootScope.currRel !== 'itself' && $scope.regionId !== 'us'){
        $scope.loadDataByTab($rootScope.currRel);
    } else if($rootScope.resourceHref !== undefined) {
        var params = {};
         resourceFactory.get($rootScope.resourceHref, params, $rootScope.headers).success(function(responseData){
            var data = responseData.data || responseData;
            if (data) {
                $scope.data=data;
                EnumerationService.executeEnumerationFromBackEnd(data, 'create');
                if($rootScope.regionId === 'us'){
                    EnumerationService.executeEnumerationFromBackEnd(data, 'fetch');    
                }
            }
        });            
    }
});