【发布时间】:2013-06-03 17:27:36
【问题描述】:
我使用$.Deferred() 等待异步调用,然后从中获取数据。
到目前为止尝试过:http://jsfiddle.net/RMSbx/1/
var result = "";
function asyncCallWrapper () {
return $.Deferred(function(def) { asyncCallFunction(arg1, function ( //Fires a callback when completed
def.resolve(data);
)))
}
$.when(asyncCallWrapper ()).done(function (dataFromAsyncCall) {
alert(dataFromAsyncCall); // Alerts proper data
result = dataFromAsyncCall;
});
alert(result);// Empty string
如何在 done() 之外访问result
【问题讨论】:
-
你需要学习如何使用 Promise。
标签: jquery asynchronous jquery-deferred