1

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

Common_Coder
  • 131
  • 6
  • I think one single version of jQuery should be enough, and you're not doing anything with the `onLoadCallback` callback. On which line are you getting the error? – Alon Eitan Feb 28 '20 at 10:31
  • @AlonEitan i tired with one version still i am getting same error , i am getting error at return new Promise line as Promise is undefined – Common_Coder Feb 28 '20 at 10:51
  • @Common_Coder Where does this code run, within Office or your browser? And what browser are you using btw? Promise isn't available in IE11 and would require a polyfill – GBWDev Feb 28 '20 at 11:40
  • @GBWDev i am developing a word web addin using it in office 365 – Common_Coder Feb 28 '20 at 12:08
  • @Common_Coder what browser do you use? – GBWDev Feb 28 '20 at 13:25
  • add this before your jquery ` ` does it then work? – GBWDev Feb 28 '20 at 13:29
  • Another option is to use jQueries promise API instead – GBWDev Feb 28 '20 at 13:30
  • Use this article to determine if your add-in is running in IE. [Browsers used by Office Web Add-ins](https://learn.microsoft.com/en-us/office/dev/add-ins/concepts/browsers-used-by-office-web-add-ins). IE does not support Promises. The fix is to run this code at the beginning of any JavaScript file that uses Promises: `if (!window.Promise) { window.Promise = Office.Promise; }` – Rick Kirkham Feb 28 '20 at 23:25

2 Answers2

0

Following is the way to create function as promise and call the function

sample cade on how a promise works and how to it can be invoked, a promise object can be invoked only by calling the then method and passing resolve and reject callback functions.

function callPromise() {

  return new Promise(function(resolve, reject) {
  // do a thing, possibly async, then…

  if (true) {
    resolve("Stuff worked!");
  }
  else {
    reject(Error("It broke"));
  }
});

}

callPromise().then(function(success){
   console.log(success);
}, function(error)
{
  console.log(error);
})



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);
    });
}

getBase64('file', function(){}).then(function(result)
{
   console.log(result);
},
function(error)
{
   console.log(error);
})
Faiz Mohammed
  • 344
  • 3
  • 11
0

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; }

Starlord
  • 99
  • 6