【发布时间】:2016-07-20 07:17:43
【问题描述】:
我是 Javascript 和 JQuery 的新手。根据我的理解, .then() 和 .done() 是由于 resolve() 在延迟对象上的结果而被触发的。但是在我的代码中,虽然没有调用 resolve(),但是 .then() 和 .done() 被触发了
<head>
<meta charset="UTF-8">
<title>Testing Promises</title>
</head>
<body>
<button type="button" onclick="test()"> Click Me</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
function test() {
var promise = $.when(another);
promise.done(function () {
console.log("another() done");
});
promise.done(function () {
console.log("another() done 2");
});
promise.done(function () {
console.log("another() done 3");
});
promise.fail(function (error) {
console.log("Failed with error = ", error);
});
promise.then(function () {
console.log("In then():");
});
console.log("test() done");
}
function another() {
var def = $.Deferred();
console.log("In another()");
return def.promise();
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript jquery html promise jquery-deferred