1

I created an Office add-in, and I'm wondering how to get the Internet headers using getAllInternetHeadersAsync? I have the below code, which will send the headers to the console:

var headers = "";
// Get the internet headers related to the mail.
Office.context.mailbox.item.getAllInternetHeadersAsync(
     function(asyncResult) {
         if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
             headers = asyncResult.value;
             console.log(headers);

         } else {
            if (asyncResult.error.code == 9020) {
              // GenericResponseError returned when there is no context.
              // Treat as no context.
         } else {
              // Handle the error.
           }
       }
     }
  );
  console.log("headers = " + headers);

However, headers doesn't seem to get permanently set. The first console.log shows the correct value for headers. The last console.log, however, reveals that headers is back to empty. How can I get headers set so that after the getAllInternetHeadersAsync function I can still see it?

Thanks!

BigDataFiles
  • 105
  • 1
  • 10
  • `headers = asyncResult.value;` that's where you can access the headers ... only there ... because asynchronous code is asynchronous ... if asynchronous code confuses you, you could put that code inside a function that returns a Promise, then call that function using await in an async function, and the code will "look" more synchronous – Jaromanda X Aug 19 '20 at 00:13
  • oh, wait ... "office" - probably wouldn't understand async/await ... – Jaromanda X Aug 19 '20 at 00:18

1 Answers1

0

Take a closer look at your console output. You should find that the console.log("headers = " + headers) output from the end of your code is displayed before the console.log(headers) output from inside the callback function.

getAllInternetHeadersAsync(), like many of the Office API functions, is an asynchronous function as the name indicates. When you call the function, it returns immediately, before getting the headers. So any code after the function call will execute right then and there. But you don't have the headers yet!

Some small amount of time later, the function gets the headers and calls your callback function. Now you have access to the headers. But it doesn't do any good to store those headers in a global variable, because your other code doesn't know when they are ready.

What you need to do instead: whatever code you have that needs to look at the headers should be right there inside the callback function, or else in another function that you call from the callback code. This way your code will have the headers available.

This is how you have to handle every Office API function with Async in the name.

As @JaromandaX notes in a comment, you could use a Promise and async/await, but you would have to create the Promise yourself as the Office API does not do this for you - it just uses a callback. Also using async/await would limit you to modern browsers that support it, or require you to use a compiler to convert your code to ES5-compatible code if you need to support Internet Explorer.

For the Office APIs it's simplest if you just stick with the callback system that the xyzAsync functions provide, and only access the asyncResult.value inside that callback or in another function that you call from inside the callback.

For more reading, a web search for asynchronous javascript will find many articles explaining this in more detail.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75