wan*_*tel 17 javascript promise ecmascript-6 reactjs
我有一个用ES6编写的React项目.它是使用Babel编译的,效果很好.除了一个承诺(很多!)只在IE中起作用,我已经知道 - 不支持承诺.于是我马上想到添加填充工具提供承诺的IE浏览器,但我很喜欢"抱起来,你已经写ES6,是不是编译成ES5反正?" 谁会比SO更清楚?
那么在我的项目中添加诸如es6-promise之类的polyfill是否有任何意义?如果有,我应该如何在语法上使用它?现在我只有导入但我应该以某种方式实现它?
import Promise from 'es6-promise';
Run Code Online (Sandbox Code Playgroud)
这也是导致IE出现问题的承诺,也许我有一个语法错误,我没有注意到自己!:)
new SingleObjectResource(DJ_CONST.API.setLanguage)
.put(null, {language_code: theLanguage})
.then(
function() {
window.location.reload();
}
);
Run Code Online (Sandbox Code Playgroud)
Dan*_*ish 13
我有同样的情况,并且非常沮丧,因为我必须部署生产应用程序,我遇到的问题是来自fetchjs的Promises.这就是我拯救生命的方法
npm install --save es6-promise //first install as a dependency & then added in broswerify as dependency.
Run Code Online (Sandbox Code Playgroud)
然后在我的主JS文件中,justed称之为
import "es6-promise/auto";
Run Code Online (Sandbox Code Playgroud)
从这里开始https://github.com/stefanpenner/es6-promise#auto-polyfill
基本上,它的替代语法
require('es6-promise').polyfill();
Run Code Online (Sandbox Code Playgroud)
基本上,在引擎盖下,polyfill()方法将在调用时修补全局环境(在本例中为Promise名称).
注意:我在浏览器中使用gulp.
我无法提前编辑我之前的回复,因为我在离线时收到了评论评论...根据评论反馈重新发布带有嵌入信息的回复。谢谢。
\n\n为什么不到处使用蓝鸟呢?它比本地承诺更快。还有IE 的 polyfills。而且我不为他们工作:)。
\n\n编辑:
\n\n使用蓝鸟代替原生承诺 -
\n\nconst Promise = require(\'bluebird\');\n
Run Code Online (Sandbox Code Playgroud)\n\nresults for 10000 parallel executions, 1 ms per I/O op\n\nfile time(ms) memory(MB)\ncallbacks-baseline.js 232 35.86\npromises-bluebird-generator.js 235 38.04\npromises-bluebird.js 335 52.08\npromises-cujojs-when.js 405 75.77\npromises-tildeio-rsvp.js 468 87.56\npromises-dfilatov-vow.js 578 125.98\ncallbacks-caolan-async-waterfall.js 634 88.64\npromises-lvivski-davy.js 653 109.64\npromises-calvinmetcalf-lie.js 732 165.41\npromises-obvious-kew.js 1346 261.69\npromises-ecmascript6-native.js 1348 189.29\ngenerators-tj-co.js 1419 164.03\npromises-then-promise.js 1571 294.45\npromises-medikoo-deferred.js 2091 262.18\nobservables-Reactive-Extensions-RxJS.js 3201 356.76\nobservables-caolan-highland.js 7429 616.78\npromises-kriskowal-q.js 9952 694.23\nobservables-baconjs-bacon.js.js 25805 885.55\n\nPlatform info:\nWindows_NT 6.1.7601 x64\nNode.JS 1.1.0\nV8 4.1.0.14\nIntel(R) Core(TM) i5-2500K CPU @ 3.30GHz \xc3\x97 4\n
Run Code Online (Sandbox Code Playgroud)\n\n2. IE Polyfill代码-
\n\nimport Bluebird from \'bluebird\';\n// Node\nglobal.Promise = Bluebird;\n// Browser\nwindow.Promise = Bluebird;\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
13644 次 |
最近记录: |