I'm developing a Word Add-in (Word API + Office.js) where i am working with content controls, i am try to check whether control is blank, if it is blank i am trying to set a flag to "False".
but because of the async nature the execution is moving to next line with out fully executing CheckMandatoryFields method, hence mandatoryflag is always true.
is there any way to wait until the execution of CheckMandatoryFields is completed
var mandatoryflag = "True";
function Test()
{
CheckMandatoryFields();
if (mandatoryflag)
{
document.getElementById('lblstatus').innerText += "Success" + " ";
}
}
function CheckMandatoryFields() {
var MadatoryFieldsList = ["Control1","Control2"];
$.each(MadatoryFieldsList, function (index, element) {
Word.run(function (context) {
var contentControls = context.document.contentControls.getByTag(element).getFirst();
contentControls.load('text');
return context.sync().then(function () {
var text = contentControls.text;
if (text == "") {
document.getElementById('lblstatus').innerText += element + " is Mandatory" + " ";
mandatoryflag = "False";
}
})
});
});
}
CheckMandatoryFieldsto return a Promise. Then put the code that should come after it in athen()method. Also, its generally not a good practice to have aWord.runin a loop. Try to loop through the array, inside theWord.run. - Rick Kirkham