简体   繁体   中英

Javascript method synchronous call without callback and $.ajax

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM