I'm exploring the Javascript API for Office (for Office 2013), and running into an odd problem. I can't seem to get variables returned up the chain of functions. Example, the API gives a way to check and see all bindings currently associated with the document, using this:
Office.context.document.bindings.getAllAsync(function (asyncResult) {});
I'm trying to use that in a function to get that data as a variable so that I can call certain bindings.
I can call my display function inside the innermost function of the API call, and use that to display the result. I can't seem to return that data upwards though.
I've tried declaring a variable in the wrapper function, outside the API call. I've tried having two return statements. I feel like this should work:
function getBindings () {
var bindingString;
Office.context.document.bindings.getAllAsync(function (asyncResult) {
for (var i in asyncResult.value) {
bindingString += asyncResult.value[i].id;
}
});
return 'Bindings: '+bindingString[0];
}
Unfortunately that just returns this:
Bindings: undefined
I know that inside the innermost function, I have data, because I've placed a call to my display function from inside, and had the bindings written to the page. I could therefore just creating a hidden holding ` that I fill and then read. I feel like that would be a hack though.
Is there a better way to return the variable?
Benjamin Gruenbaum answered this as a comment, so I'm adding it here to make it easy to find in case anyone else ever has this issue.
You cannot return the response of an asynchronous call synchronously (it's not there yet), so you invoke a method with that value, in that method, you can use its value - this is called continuation passing style. Alternatively, you can return a proxy for the object instead of the object itself - and then use it when it's ready via a
then
method, this is called a promise
So unfortunately the answer is that it cannot be done.