buzzdriving
buzzdriving

Reputation: 43

MS Office Add-Ins, JavaScript and async/await

I am currently trying to get up to speed on writing an add-in for Excel and have done loads of reading around on the subject and am following this excellent tutorial series.

In the example, the presenter is using TypeScript, however, the current project I'm working on requires JavaScript.

I have replicated the example in a JavaScript Excel Web Add-In project in Visual Studio 2019 Community Edition instead of Script Lab and debugging the add-in on Desktop Excel seems to be working.

My problem comes in when using the async/await syntax of the TypeScript example in my JavaScript version, as the debugger throws a syntax error when I use it. Not having much experience with asynchronous programming in JS, I can see from the outset why async/await is much easier to use than nested callbacks or Promises. Chaining up thens looks torturous to me and I'd rather use async/await if I can.

What gave me the run around for a while was that JavaScript apparently does support async/await. So, the only thing I can think of now, is that the version of JavaScript that Excel is running is pre-ECMAScript 8. I can't find any Office.js add-in examples using async/await in JS. I don't know how the architecture all hangs together but if someone could confirm this, I'd appreciate it because I'd rather hear that there's a problem with the JavaScript version and I therefore can do nothing about it than continue under a false assumption and miss out on the chance to use async/await. [Sidenote: I'm also aware of OfficeExtension.Promise but that's still unsavory).

Thanks in advance.

Upvotes: 1

Views: 1563

Answers (1)

Rick Kirkham
Rick Kirkham

Reputation: 9784

If you are getting syntax errors from await/async, then you are probably testing on a version of Office that uses IE as the browser. (For information on which browser is used on various versions of Office, see Browsers used by Office Add-ins.) IE does not natively support any version of JavaScript past ES5, so it doesn't recognize async/await (or Promises). To use these features of ES6+ when the browser is IE, you need to load a polyfill or else use a transpiler like tsc or babel to transpile the ES6+ code down to ES5 code.

Upvotes: 1

Related Questions