Search code examples
javascriptpromiseoffice-jsword-addinsword-web-addins

Getting promise undefined error when i use Promise in my word addin application


I'm developing a Word Add-in (Word API + Office.js),i am trying to implement a method as promise but i am getting error stating promise is undefined

These are reference which i am using

<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script src="Scripts/FabricUI/MessageBanner.js" type="text/javascript"></script>
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>

This is method where i am trying to return a promise

function getBase64(file, onLoadCallback) {
    return new Promise(function (resolve, reject) {
        var reader = new FileReader();
        reader.onload = function () { resolve(reader.result); };
        reader.onerror = reject;
        reader.readAsDataURL(file);
    });
}

i tried creating a sample promise method from web tutorial in my addin project even there i am getting same error. please let me know whether promise are supported in word addin !! or if i am missing something


Solution

  • That error means your add-in is running in IE which does not natively support Promises. Office has a Promises polyfill that you can use. Just add this code to the top of the JavaScript file: if (!window.Promise) { window.Promise = Office.Promise; }