Adding promise polyfill to ES6 Adding promise polyfill to ES6 reactjs reactjs

Adding promise polyfill to ES6


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

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

   import "es6-promise/auto";

as from here 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.

Note: i was using gulp with browserify.


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 bluebird everywhere? Its faster than the native promises. And polyfills for IE too. And I don't work for them :).

EDIT:

Using bluebird instead of native promise -

const Promise = require('bluebird');

1. Added perf comparisons -

results for 10000 parallel executions, 1 ms per I/O opfile                                     time(ms)  memory(MB)callbacks-baseline.js                         232       35.86promises-bluebird-generator.js                235       38.04promises-bluebird.js                          335       52.08promises-cujojs-when.js                       405       75.77promises-tildeio-rsvp.js                      468       87.56promises-dfilatov-vow.js                      578      125.98callbacks-caolan-async-waterfall.js           634       88.64promises-lvivski-davy.js                      653      109.64promises-calvinmetcalf-lie.js                 732      165.41promises-obvious-kew.js                      1346      261.69promises-ecmascript6-native.js               1348      189.29generators-tj-co.js                          1419      164.03promises-then-promise.js                     1571      294.45promises-medikoo-deferred.js                 2091      262.18observables-Reactive-Extensions-RxJS.js      3201      356.76observables-caolan-highland.js               7429      616.78promises-kriskowal-q.js                      9952      694.23observables-baconjs-bacon.js.js             25805      885.55Platform info:Windows_NT 6.1.7601 x64Node.JS 1.1.0V8 4.1.0.14Intel(R) Core(TM) i5-2500K CPU @ 3.30GHz × 4

2. IE Polyfill code -

import Bluebird from 'bluebird';// Nodeglobal.Promise = Bluebird;// Browserwindow.Promise = Bluebird;


Although you are using Babel (just traspiling and not adding functionality), polyfills are needed.

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)

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