0

I have an outlook javascript function where I am trying to get the value of MyMessage into the console.log outside of the full function, however I am having trouble.

function confirmedsendheaders() {
  Office.context.mailbox.item.getAllInternetHeadersAsync(function (asyncResult) {
     if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
     MyMessage = `Hello Support, <br>
<br>
This is an automated email from` + Office.context.mailbox.userProfile.displayName + ` to check if this is spam. <br>
<br>      
<b>Email Header Information: </b>
<br>
`+ asyncResult.value
      return MyMessage

    }

  })
}

I also tried putting the return right next to the variable in question but no luck:

function confirmedsendheaders() {
  Office.context.mailbox.item.getAllInternetHeadersAsync(function (asyncResult) {
     if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
     return MyMessage = `Hello Support, <br>
<br>
This is an automated email from` + Office.context.mailbox.userProfile.displayName + ` to check if this is spam. <br>
<br>
<b>Email Header Information: </b>
<br>
`+ asyncResult.value
      

    }

  })
}

I read this: Return value from nested function in Javascript and it mentions that the second function needs to be called, but it looks like it gets called at the beginning unless I am wrong. Any ideas?

Edit: I also tried to have a variable equal the same result from the nested function to see if that work but no luck. I saw the idea from this post:https://stackoverflow.com/a/3373953

third attempt:

function confirmedsendheaders() {
var secret = ''
  Office.context.mailbox.item.getAllInternetHeadersAsync(function (asyncResult) {
     if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
     MyMessage = `Hello Support, <br>
<br>
This is an automated email from` + Office.context.mailbox.userProfile.displayName + ` to check if this is spam. <br>
<br>
<b>Email Header Information: </b>
<br>
`+ asyncResult.value
      secret = MyMessage.secret

    }

  })
return secret
}

This is not a async function. I tried to add await and it said: Uncaught SyntaxError: **await is only valid in async** functions and the top level bodies of modules Fourth attempt

function confirmedsendheaders() {
var secret = ''
  Office.context.mailbox.item.getAllInternetHeadersAsync(function (asyncResult) {
     var secret = await if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
     MyMessage = `Hello Support, <br>
<br>
This is an automated email from` + Office.context.mailbox.userProfile.displayName + ` to check if this is spam. <br>
<br>
<b>Email Header Information: </b>
<br>
`+ asyncResult.value
    }

  })
return secret
}
  • this is an ayync function? – Tom Foster Oct 26 '21 at 02:50
  • This is an async function with a callback, it most likely isn't returning a promise that you can `await` on. See the heading _"Let functions accept callbacks"_ section from the link above. The idea isn't to return the value from your function, but to pass it into a callback function that you pass to `confirmedsendheaders()` – Nick Parsons Oct 26 '21 at 03:45

0 Answers0