0

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...

Laureant
  • 979
  • 3
  • 18
  • 47
  • Make a new call when the preceding call's readyState changes and in the final asynchronous call return the tastdata object. May be !!! – Think Different Oct 14 '15 at 10:48
  • Ok, I nested the async calls into one another, but stil... it's not working properly. In the main function, where I call this GetAllFields() function, it goes forward, and it's not waiting for it, to finish. – Laureant Oct 14 '15 at 11:07

0 Answers0