Search code examples
javascriptpromiserequirejshtml2canvas

How can I get promises to work in IE?


In my html, I have:

<script type="text/javascript" src="js/libs/require.js"></script>
<script type="text/javascript" src="js/libs/html2canvas/html2canvas-master/dist/html2canvas.js"></script>

To solve other issues I had to upgrade to the version of html2canvas that uses Promises, but found that this is supported in IE (11).

I downloaded the library "es6-promise-master" as suggested by the maker of html2canvas. This is located in js/libs/es6-promise-master.

In my main javascript file, I have:

var Promise = require(['es6-promise']).Promise;

I cannot get this to work. My latest error in javascript is:

require.js: Line: 7
Error: Mismatched anonymous define() module: function(c){return b(a,c)}
http://requirejs.org/docs/errors.html#mismatch

I have read http://requirejs.org/docs/errors.html#mismatch but I really need some code to figure out how to get this working.


Solution

  • This syntax:

    var Promise = require(['es6-promise']).Promise;
    

    assumes you have a CommonJS environment available. This is provided by tools like Webpack and Browserify.

    You're using RequireJS at the moment - this is something different than CommonJS and uses a different format for modules.

    The easiest way to use that Promise implementation would be to include a polyfill that will automatically install itself when you include the script in your page. This is provided as es6-promise.auto.js.