2
function sync(){
var timer;
$('#result').html('waiting…');
var promise = process();
promise.done(function(a) {
$('#result').html('done.'+a);
});
promise.fail(function(e) {
$('#result').html('fail.'+e);
});
}
function process() {
var deferred = $.Deferred();
var url = "https://maps.googleapis.com/maps/api/geocode/json";
$.ajax({
url: url,
data: {},
success: function(data){
return deferred.resolve(5); // line:1
},
error: function(data){
return deferred.reject(0); // line:2
}
});
return deferred.promise(); // line:3
}
爲什麼要在上面的代碼中返回deferred.promise()
?假設如果我刪除線#3,然後我得到這樣一個錯誤:我們爲什麼要返回deferred.promise()
TypeError: promise is undefined
應該在line#1
或line#2
返回。爲什麼我們需要line#3
以及第3行的用途是什麼?
if it is return properly at line#1 or Line#2 then What is the use of line#3 see the code here