Can't find variable: Promise on Safari

2020-02-12 06:28发布

问题:

I have a site I'm working on that runs perfectly on Chrome for the desktop (Windows 8.1 & OS X Mavericks)

When I run it on iOS 7 or Safari 7.0.2 I get an error to the console that states 'Error while loading route: checkIfLoggedIn'

the member it specifies at the message is not a route, it is a method that returns a promise. When I debug through the ember code to figure out what is going wrong I found that it is rejecting the promise with the reason of 'Can't find variable: Promise'

I can't post the actual code from my site here, so I set out to create a fiddle that reproduces the error and I was able to come up with this:

http://jsfiddle.net/NQKvy/851/

This runs perfectly on Chrome for the desktop (Windows 8.1 & OS X Mavericks), but on iOS 7 or Safari 7.0.2 throws the following error to the console 'ReferenceError: Can't find variable: Promise'

Anyone have any ideas why this isn't working?

To recap:

  • I have tested on Chrome for Windows 8.1 and OS X Mavericks - It works
  • I have tested on Chrome for iOS and it does not work
  • I have tested on Safari for iOS and OS X Mavericks and it does not work
  • I have not tested on Android at all (I don't have access to any devices at this moment)

This leads me to believe that it is a Safari error as (if I recall correctly) Chrome for iOS uses a Safari control to render the page rather than Chromium

This is the code I'm using to generate the error:

App.ready = function() {
    var asdf = new Promise(function (resolve) {   
        var i = 1;
        i++;
        resolve.call(this,i);
    }).then(function (result) {
        alert('I: ' + result);
    });
};

回答1:

Turns out that on Safari you must use the fully qualified name when creating a promise, otherwise it will not work:

App.ready = function() {
    var asdf = new Ember.RSVP.Promise(function (resolve) {   
        var i = 1;
        i++;
        resolve.call(this,i);
    }).then(function (result) {
        alert('I: ' + result);
    });
};

Note the 'new Ember.RSVP.Promise' instead of the 'new Promise'. This appears to fix it for me.



回答2:

According to this, there are no promises for iOS Safari 7.1, or Safari 7.

However, they will be getting them in iOS Safari 8 and Safari 8.