I have to make a lot of asynchronous calls to an application, to get some fields (from Microsoft Project).
This is how an async call looks like
Office.context.document.getTaskFieldAsync(GUID, Office.ProjectTaskFields.Start,
function (result) {
if (result.status === Office.AsyncResultStatus.Succeeded) {
taskdata.taskStart = result.value.fieldValue;
}
else {
app.showNotification('Error 33:', result.error.message);
}
}
);
I want to have a subfunction that looks like the following :
function GetAllFields(GUID) {
var taskdata;
Office.context.document.getTaskFieldAsync(GUID,Name....
....
taskdata.Name= result.value.fieldValue;
....
);
Office.context.document.getTaskFieldAsync(GUID,Start....
....
taskdata.Start = result.value.fieldValue;
....
);
Office.context.document.getTaskFieldAsync(GUID,Finish....
....
taskdata.Finish= result.value.fieldValue;
....
);
return taskdata
}
The problem is, that the taskdata object is returned null before any of the async calls firing, and that's normal behaviour, but I need a great way of chaining these events, or returning only if all the async calls were done.
UPDATE
After nesting the async calls into one another, and returning the taskdata object only in the deepest call, i thought it would fix the issue, but in the parent function, where I call GetAllFields(GUID), it goes forward, and it's not waiting for it to return...