【发布时间】:2015-09-04 01:50:28
【问题描述】:
为什么这段代码不起作用?
它应该在加载第 2 步之前等待第 1 步加载。目前,第 2 步首先触发。我正在使用 mockjax 来模拟 Ajax 调用。
$.mockjax({
url: "/step1",
responseTime: [3000, 4000],
responseText: {
status: "success",
text: "Loading Step 1"
}
});
$.mockjax({
url: "/step2",
responseTime: [100, 200],
responseText: {
status: "success",
text: "Loading step 2"
}
});
$.getJSON("/step1").then( function(response) {
return $("#message").html( "message: " + response.text );
})
.then(
$.getJSON("/step2", function(response) {
$("#message").html( "message: " + response.text );
})
)
【问题讨论】:
-
.then的参数应该是一个函数。 -
我在任何地方都没有看到 ES6 承诺?
-
这与 ES6 Promise 无关。
-
jQuery 承诺是邪恶的。用
Promise.cast()杀死他们。
标签: javascript jquery ajax jquery-deferred