Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a Promise is supported by the browser

Does anyone know, using Modernizr or otherwise, if there is a way to detect if the Promise feature is enabled in a browser?

I have a polyfill for the functionality, but only want to apply it if the browser does not have a native implementation.

like image 313
user2836501 Avatar asked Mar 19 '14 20:03

user2836501


People also ask

Is Promise supported by all browsers?

Update Dec 11 - 2016: All evergreen versions of browsers now support promises. They are safe to use. Update Nov 14 - 2016: Chrome, Firefox, Safari and IE all now have experimental support for promises in their dev channels.

Is Promise part of JavaScript or browser?

As of ES6 (ES2015), promises are built into the Javascript specification and implementations to that spec.

Is Promise supported in Internet Explorer?

'Promise' is undefined in Internet Explorer (IE 11)

Can I use Promises in IE11?

IE11 neither supports arrow functions nor native Promises. Use a JS transpiler (like babel) or don't use ES6 features. For Promise support you can use a library like bluebird.


4 Answers

Update Dec 11 - 2016: All evergreen versions of browsers now support promises. They are safe to use.


Update Nov 14 - 2016: Chrome, Firefox, Safari and IE all now have experimental support for promises in their dev channels. The specification has settled. I would still not rely on the implementation just yet and would use a library but this might change in a few months.


No browsers support promises natively in a reliable way. The specification might change - at least for a few more months. My suggestion is use a fast promise library like Bluebird.

If you want to check if native promises are enabled - you can do :

if(typeof Promise !== "undefined" && Promise.toString().indexOf("[native code]") !== -1){
    //here
}

As others suggested, just checking if there is a Promise object can be done by if(Promise) but I strongly suggest against it since different libraries have different APIs for creation of promises etc.

like image 97
Benjamin Gruenbaum Avatar answered Oct 18 '22 04:10

Benjamin Gruenbaum


Not so fast.

This throws when "Promise" is undefined:

if (Promise)
  // do code

This never throws:

if (window.Promise)
  // do code

if (typeof Promise !== 'undefined')
  // do code

and yes the window object can be relied upon in a browser environment.

like image 39
King Friday Avatar answered Oct 18 '22 05:10

King Friday


To create a 'supported' flag without carrying around an object reference:

var canPromise = !! window.Promise;
like image 6
Paul Roub Avatar answered Oct 18 '22 05:10

Paul Roub


You could try to create one in a try/catch block:

var promiseSupport = false;
try {
    var promise = new Promise(function (x, y) {});
    promiseSupport = true;
} catch (e) {}

Check promiseSupport to see whether or not it fails.

JSFiddle

like image 3
War10ck Avatar answered Oct 18 '22 03:10

War10ck