'Promise' is undefined error when run on IE

有些话、适合烂在心里 提交于 2021-01-27 12:27:38

问题


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?


回答1:


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');    
                }
            }
        });            
    }
});



回答2:


Native promise is an ES6 feature which is not supported by old browsers. You need to add a polyfill to support old browsers, for example https://github.com/taylorhakes/promise-polyfill.

Since you're using AngularJS you can use the $q service instead to create promises which work cross browsser.




回答3:


https://github.com/stefanpenner/es6-promiseCorrect me if I'm wrong, but it seems to me that you're using the native implementation of Promise, which is not supported by IE (yet).

In this case, try using a polyfill.

Also, you may use Angular's $q service, which is a Promisse Pattern implementation based on kriskowal's Q module.

Example:

$q(function (resolve, reject) {
    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');    
                }
            }
        });            
    }
});


来源:https://stackoverflow.com/questions/38069665/promise-is-undefined-error-when-run-on-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!