0
\$\begingroup\$

I'm working on an Outlook Web Addin that gets the email body. So in the Office API you could get the email body in two types: Simple Text and Html.

Our requirement is to get the HTML format so this is easy, however, even if the email body is empty the HTML format still returns a value which is the HTML elements but does not have contents in it. So my solution is to check first for the Simple Text version of the email body then if there is a content get the HTLM format version.

Please see the below codes:

var mailItem = Office.context.mailbox.item;

mailItem.body.getAsync(Office.CoercionType.Text, function (result) {
    if (result.status === Office.AsyncResultStatus.Succeeded) {
        var normalizeValue = null;

        if (result.value) {
            normalizeValue = result.value.trim();
        }

        if (normalizeValue !== '') {
            mailItem.body.getAsync(Office.CoercionType.Html, function (result) {
                if (result.status === Office.AsyncResultStatus.Succeeded) {
                    // the value will be initialized in input value
                    $('#body').val(result.value.trim());
                }
            });
        }
    }
});

I would like to get the value from the 'Text' type and pass this in a variable then check the variable then run the getAsync for the html version separately instead of having the codes inside the method of body.getAsync. Or anything the code could improve?

\$\endgroup\$
  • 2
    \$\begingroup\$ Is this really a code review question? It seems, from the text, that you don't have a working piece of code yet, so it could be more appropriate to have this on SO? \$\endgroup\$ – Icepickle Aug 14 '17 at 9:47
  • \$\begingroup\$ Could you include a scenario where we would see the difference in results yielded from the different Office.CoercionType types? As it currently stands, your question is off-topic due to lack of context. We can only guess the content of the results. \$\endgroup\$ – dfhwze Sep 3 at 17:10
1
\$\begingroup\$

I would suggest you could use Promise.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

If you don't want additional library for Promise, you could use Deferred from jQuery also. https://api.jquery.com/deferred.promise/

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.