0
myapp.Calc.SkipOrder_Tap_canExecute = function (screen) {
    var enabledBool = new Boolean;
    screen.getOrders().then(function (result) {
        enabledBool = (screen.OrderPosition < result.data.length - 1);
    });
    return enabledBool;
};

The SkipOrder_Tap is a button object in MS LightSwitch. The problem is, I need to return the Boolean AFTER the asynchronous call.

3
  • 1
    possible duplicate of How to return the response from an AJAX call? Commented Mar 8, 2014 at 18:23
  • You are almost there. You just have to return the promise from the function, and then call it as myapp.Calc.FirstOrder_Tap_canExecute().then(function(enabled) { ...});. Learn more about .then: api.jquery.com/deferred.then Commented Mar 8, 2014 at 18:25
  • I updated the code to show usage of the getOrders result Commented Mar 8, 2014 at 18:44

1 Answer 1

2

The idea of the promises is to handle asynchronous tasks.

I guess that screen.getOrders() makes asynchronous request and returns a promise. When you use then and pass a callback you will get the orders once the promise is resolved i.e. when your callback passed to then is invoked.

You can proceed as follows, using the promises "chaining":

myapp.Calc.FirstOrder_Tap_canExecute = function (screen) {
  return screen.getOrders().then(function (result) {
    return (screen.OrderPosition != 0);
  });
};

myapp.Calc.FirstOrder_Tap_canExecute()
.then(function (booleanResult) {
   //do whatever you need
});
Sign up to request clarification or add additional context in comments.

4 Comments

I see what you mean! I'm using MS LightSwitch and the FirstOrder_Tap is a button object. If I were to put "return false;" as the only statement in the canExecute function, the button is disabled. I don't see why your edit doesn't work, but the button is active :(
@borkith: If you imply that myapp.Calc.SkipOrder_Tap_canExecute should be a synchronous function, than that just wouldn't work.
I updated the code to show that the "result" is the fulfilled promise data from getOrders
I tried "var orders = screen.getOrders();" but it doesn't return the .data I get from "screen.getOrders().then(function (result) {})". I also don't call the canExecute(), it's done by LightSwitch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.