1

Goal:

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.

Problem:

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.

What I've Tried:

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.

Question:

Is there a better way to return the variable?

Community
  • 1
  • 1
Kelderic
  • 6,502
  • 8
  • 46
  • 85
  • Have you read http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call ? – Benjamin Gruenbaum Jun 12 '14 at 22:58
  • Hmm, that seems to indicate that as long as we are doing Async, we can only do downwards, not upwards. – Kelderic Jun 12 '14 at 23:01
  • Andy, yes. That is correct. Summing up from my answer there - 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. – Benjamin Gruenbaum Jun 12 '14 at 23:01
  • Unfortunately the API only supports promises for 4 calls, and getAllAsync isn't one of them. – Kelderic Jun 12 '14 at 23:11
  • 1
    You can always [make your own](http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) but I'm afraid that this is how concurrency works in JavaScript, once you go async - you have to stay async. You can't simply return the response of an asynchronous call (well, yet). – Benjamin Gruenbaum Jun 12 '14 at 23:12
  • Benjamin, if you'd like to write up that answer in a post, with something like, you can't, because it's using async, which can't do this because of so and so, and for more info see here, I'll select it as the answer to help other people find it more easily. – Kelderic Jun 14 '14 at 16:07

1 Answers1

0

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.

Community
  • 1
  • 1
Kelderic
  • 6,502
  • 8
  • 46
  • 85