如何确定浏览器是否支持Promise [英] How to determine if a Promise is supported by the browser

查看:922
本文介绍了如何确定浏览器是否支持Promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道,使用Modernizr或其他方式,是否可以检测是否已在浏览器中启用了Promise功能?

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.

推荐答案

更新12月11日- 2016年:现在所有常绿版本的浏览器都支持promises。它们可以安全使用。

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

2016年11月14日更新: Chrome,Firefox,Safari和IE现在都在其开发渠道中对Promise进行了实验性支持。规范已确定。我现在仍然不会依赖于实现,而是会使用库,但这可能会在几个月后发生变化。

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.

没有浏览器以可靠的方式原生支持诺言。规范可能会更改-至少要再几个月。我的建议是使用像Bluebird这样的快速承诺库。

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
}

如其他人所建议的,只需检查是否存在是 Promise 对象可以通过 if(Promise)完成,但我强烈建议不要这样做,因为不同的库具有不同的API来创建承诺等。

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.

这篇关于如何确定浏览器是否支持Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆