0

Welcome,

I want to call synchronous javascript method without callbacks. The method is a POST asynchronous.

function someMethod() {
    var bean; //bean is a proxy object provide by external library
    if(clicked_onSpecial_field) {
        if(!bean.isAuthorized()) { //here is a async call
            return false;
        }  
    }

    //a lot of other instructions... 

}

From one hand usually clicked_onSpecial_field = false so I cannot put all other instructions in callback. From the another hand bean is a provide to me proxy object. In this case i don't know in which way I can use $.ajax.

Could you please help?

Martinus_
  • 130
  • 1
  • 3
  • 12
  • How do you normally get the result back from bean.isAuthorized()? And can this result change based on things that happened earlier during the page usage? – Nzall Nov 19 '14 at 11:06
  • It is a first time when i call this method. I prefer to check in every time because in theory authorization status can be changed in time. But thank you hidden suggestion to load this variable on page load :) – Martinus_ Nov 19 '14 at 11:15

1 Answers1

1

I want to call synchronous javascript method without callbacks. The method is a POST asynchronous

If the method is inherently asynchronous, you cannot call it so that it returns synchronously. That's just outright impossible. See How do I return the response from an asynchronous call? for details.

usually clicked_onSpecial_field = false so I cannot put all other instructions in callback.

Of course you can!

function someMethod() {
    var bean; //bean is a proxy object provide by external library
    if (clicked_onSpecial_field) {
        bean.isAuthorized(goOn); // pass goOn as callback
    } else {
        goOn(true); // just call it directly if there's no async action
    }

    function goOn(cond) {
        //a lot of other instructions... 
    }
}

Notice that someMethod is now asynchronous as well, so if you need to pass any results from it then you'd need to use a callback there as well.

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164