欢迎,
我想调用同步 javascript 方法而不需要回调。 该方法是一个 POST 异步方法。
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...
}
通常是从一只手clicked_onSpecial_field = false
所以我不能将所有其他指令放入回调中。
另一方面,bean
是一个提供给我的代理对象。在这种情况下,我不知道如何使用 $.ajax
。
你能帮忙吗?
最佳答案
I want to call synchronous javascript method without callbacks. The method is a POST asynchronous
如果该方法本质上是异步的,则无法调用它以使其同步返回。这根本就是不可能的事。请参阅How do I return the response from an asynchronous call?了解详情。
usually
clicked_onSpecial_field = false
so I cannot put all other instructions in callback.
当然可以!
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...
}
}
请注意,someMethod
现在也是异步的,因此如果您需要从中传递任何结果,那么您还需要在那里使用回调。
关于Javascript方法同步调用,无需回调和$.ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27015107/