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