javascript 将 promise polyfill 添加到 ES6
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34307165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding promise polyfill to ES6
提问by wanaryytel
I have a React project written in ES6. It is compiled using Babel and works quite well. Except for one promise (of many!) that acts up only in IE, for which I already know - has no support for promises. So I immediately thought to add a polyfill to supply promises for IE, but then I was like "Hold up, you're already writing ES6 and isn't that compiled into ES5 anyways?" Who would know better than SO?
So is there any sense in adding a polyfill such as es6-promiseto my project? And if there is, how should I use it syntactically? For now I only have the import but I should probably implement it somehow as well?
我有一个用 ES6 编写的 React 项目。它是使用 Babel 编译的,运行良好。除了一个仅在 IE 中起作用的承诺(许多!),我已经知道 - 不支持承诺。所以我立即想到添加一个 polyfill 来为 IE 提供 promises,但后来我就像“等一下,你已经在写 ES6 了,它不是编译成 ES5 吗?” 谁会比 SO 更了解?
那么在我的项目中添加诸如es6-promise 之类的polyfill有什么意义吗?如果有,我应该如何在语法上使用它?现在我只有导入,但我也应该以某种方式实现它?
import Promise from 'es6-promise';
Also here's the promise that causes problems in IE, perhaps I have a syntax error that I haven't noticed myself! :)
另外这里的promise导致IE出现问题,可能是我自己没有注意到的语法错误!:)
new SingleObjectResource(DJ_CONST.API.setLanguage)
.put(null, {language_code: theLanguage})
.then(
function() {
window.location.reload();
}
);
回答by Danish
I had the same situation & was very frustrated as i had to deploy production app, The problem i had was with Promises from fetchjs. This is what i do to save my life
我遇到了同样的情况并且非常沮丧,因为我不得不部署生产应用程序,我遇到的问题是来自 fetchjs 的 Promises。这就是我为挽救我的生命所做的
npm install --save es6-promise //first install as a dependency & then added in broswerify as dependency.
and then in my main JS file, justed called this
然后在我的主 JS 文件中,justed 调用了这个
import "es6-promise/auto";
as from here https://github.com/stefanpenner/es6-promise#auto-polyfill
从这里开始https://github.com/stefanpenner/es6-promise#auto-polyfill
basically, its alternative syntax of
基本上,它的替代语法
require('es6-promise').polyfill();
Basically, Under the hood The polyfill() method will patch the global environment(in this case to the Promise name) when called.
基本上,在后台 polyfill() 方法将在调用时修补全局环境(在本例中为 Promise 名称)。
Note: i was using gulp with browserify.
注意:我在 browserify 中使用了 gulp。
回答by hazardous
I couldn't edit my previous response earlier as I received the review comment in the night when I was offline... re-posting my response with embedded information per the review feedback. Thanks.
我无法更早地编辑我之前的回复,因为我在离线的那天晚上收到了评论评论......根据评论反馈重新发布我的回复以及嵌入的信息。谢谢。
Why not use bluebirdeverywhere? Its faster than the native promises. And polyfills for IEtoo. And I don't work for them :).
为什么不在任何地方使用蓝鸟?它比本地承诺更快。还有IE 的 polyfills。我不为他们工作:)。
EDIT:
编辑:
Using bluebird instead of native promise -
使用 bluebird 而不是本机承诺 -
const Promise = require('bluebird');
1. Added perf comparisons -
1. 添加了性能比较 -
results for 10000 parallel executions, 1 ms per I/O op
file time(ms) memory(MB)
callbacks-baseline.js 232 35.86
promises-bluebird-generator.js 235 38.04
promises-bluebird.js 335 52.08
promises-cujojs-when.js 405 75.77
promises-tildeio-rsvp.js 468 87.56
promises-dfilatov-vow.js 578 125.98
callbacks-caolan-async-waterfall.js 634 88.64
promises-lvivski-davy.js 653 109.64
promises-calvinmetcalf-lie.js 732 165.41
promises-obvious-kew.js 1346 261.69
promises-ecmascript6-native.js 1348 189.29
generators-tj-co.js 1419 164.03
promises-then-promise.js 1571 294.45
promises-medikoo-deferred.js 2091 262.18
observables-Reactive-Extensions-RxJS.js 3201 356.76
observables-caolan-highland.js 7429 616.78
promises-kriskowal-q.js 9952 694.23
observables-baconjs-bacon.js.js 25805 885.55
Platform info:
Windows_NT 6.1.7601 x64
Node.JS 1.1.0
V8 4.1.0.14
Intel(R) Core(TM) i5-2500K CPU @ 3.30GHz × 4
2. IE Polyfill code -
2. IE Polyfill 代码 -
import Bluebird from 'bluebird';
// Node
global.Promise = Bluebird;
// Browser
window.Promise = Bluebird;
回答by tomericco
Although you are using Babel (just traspiling and not adding functionality), polyfills are needed.
尽管您使用的是 Babel(只是传输而不是添加功能),但仍需要使用 polyfill。
All you have to do is installing the package:
您所要做的就是安装软件包:
npm install --save es6-promise
Inside webpack.config.js
(or wherever your webpack configurations are, assuming you are using webpack)
内部webpack.config.js
(或 webpack 配置所在的任何地方,假设您正在使用 webpack)
require('es6-promise').polyfill();
The polyfill() method will patch the global environment (in this case to the Promise name) when called. More info at https://github.com/stefanpenner/es6-promise
polyfill() 方法将在调用时修补全局环境(在本例中为 Promise 名称)。更多信息请访问https://github.com/stefanpenner/es6-promise